ArticleslgStudy

mathematics

Bitwise operation

Bitwise operation is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bitwise operation rather than just read about it. In short: In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor.

Bitwise operation — main illustration
Bitwise operation — illustration

Key takeaways

  • Bitwise operation belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bitwise operation to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bitwise operation from memory before moving on to harder problems.

Reference excerpt

In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor. Most architectures provide only a few high value bitwise operations, presented as two-operand instructions where the result replaces one of the input operands. On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition. While modern processors usually perform addition and multiplication just as fast as bitwise operations due to their longer instruction pipelines and other architectural design choices, bitwise operations do commonly use less power because of the reduced use of resources.

Bitwise operators In the explanations below, any indication of a bit's position is counted from the right (least significant) side, advancing left. For example, the binary value 0001 (decimal 1) has zeroes at every position but the first (i.e., the rightmost) one.

NOT

The bitwise NOT, or bitwise complement, is a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Bits that are 0 become 1, and those that are 1 become 0. For example:

NOT 0111 (decimal 7) = 1000 (decimal 8)

NOT 10101011 (decimal 171) = 01010100 (decimal 84)

The result is equal to the two's complement of the value minus one. If two's complement arithmetic is used, then NOT x = -x − 1. For unsigned integers, the bitwise complement of a number is the "mirror reflection" of the number across the half-way point of the unsigned integer's range. For example, for 8-bit unsigned integers, NOT x = 255 - x, which can be visualized on a graph as a downward line that effectively "flips" an increasing range from 0 to 255, to a decreasing range from 255 to 0. A simple but illustrative example use is to invert a grayscale image where each pixel is stored as an unsigned integer.

AND

A bitwise AND is a binary operation that takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0 and 0 × 0 = 0). For example:

0101 (decimal 5) AND 0011 (decimal 3) = 0001 (decimal 1)

The operation may be used to determine whether a particular bit is set (1) or cleared (0). For example, given a bit pattern 0011 (decimal 3), to determine whether the second bit is set we use a bitwise AND with a bit pattern containing 1 only in the second bit:

0011 (decimal 3) AND 0010 (decimal 2) = 0010 (decimal 2)

Because the result 0010 is non-zero, we know the second bit in the original pattern was set. This is often called bit masking. (By analogy, the use of masking tape covers, or masks, portions that should not be altered or portions that are not of interest. In this case, the 0 values mask the bits that are not of interest.) The bitwise AND may be used to clear selected bits (or flags) of a register in which each bit represents an individual Boolean state. This technique is an efficient way to store a number of Boolean values using as little memory as possible. For example, 0110 (decimal 6) can be considered a set of four flags numbered from right to left, where the first and fourth flags are clear (0), and the second and third flags are set (1). The third flag may be cleared by using a bitwise AND with the pattern that has a zero only in the third bit:

0110 (decimal 6) AND 1011 (decimal 11) = 0010 (decimal 2)

Because of this property, it becomes easy to determine the even/odd status of a binary number by checking the value of the lowest valued bit. Using the example above:

0110 (decimal 6) AND 0001 (decimal 1) = 0000 (decimal 0)

Because 6 AND 1 is zero, 6 is divisible by two and therefore even.

OR

A bitwise OR is a binary operation that takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 0 if both bits are 0, while otherwise the result is 1. For example:

0101 (decimal 5) OR 0011 (decimal 3) = 0111 (decimal 7)

The bitwise OR may be used to set to 1 the selected bits of the register described above. For example, the fourth bit of 0010 (decimal 2) may be set by performing a bitwise OR with the pattern with only the fourth bit set:

0010 (decimal 2) OR 1000 (decimal 8) = 1010 (decimal 10)

XOR

A bitwise XOR is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only one of the bits is 1, but will be 0 if both are 0 or both are 1. In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same. For example:

0101 (decimal 5) XOR 0011 (decimal 3) = 0110 (decimal 6)

The bitwise XOR may be used to invert selected bits in a register (also called toggle or flip). Any bit may be toggled by XORing it with 1. For example, given the bit pattern 0010 (decimal 2) the second and fourth bits may be toggled by a bitwise XOR with a bit pattern containing 1 in the second and fourth positions:

0010 (decimal 2) XOR 1010 (decimal 10) = 1000 (decimal 8)

This technique may be used to manipulate bit patterns representing sets of Boolean states. Assembly language programmers and optimizing compilers sometimes use XOR as a short-cut to setting the value of a register to zero. Performing XOR on a value against itself always yields zero, and on many architectures this operation requires fewer clock cycles and less memory than loading a zero value and saving it to the register. If the set of bit strings of fixed length n (i.e. machine words) is thought of as an n-dimensional vector space 𝐅2n over the field 𝐅2, then vector addition corresponds to the bitwise XOR.

Mathematical equivalents Assuming ⁠x≥y⁠, for non-negative integers, the bitwise operations can be written as follows: NOT⁡(x)=∑n=0⌊log2x⌋2n((⌊x2n⌋mod2+1)mod2)=(2⌊log2x⌋+1−1)−xxAND⁡y=∑n=0⌊log2x⌋2n(⌊x2n⌋mod2)(⌊y2n⌋mod2)xOR⁡y=∑n=0⌊log2x⌋2n(⌊x2n⌋mod2+⌊y2n⌋mod2−(⌊x2n⌋mod2)(⌊y2n⌋mod2))xXOR⁡y=∑n=0⌊log2x⌋2n((⌊x2n⌋+⌊y2n⌋)mod2)

… excerpt ends here. Continue reading the full article.

Illustrations

Bitwise operation: Bitwise OR of 4-bit integers
Bitwise OR of 4-bit integers
Bitwise operation: Bitwise XOR of 4-bit integers
Bitwise XOR of 4-bit integers
Bitwise operation: Left arithmetic shift
Left arithmetic shift
Bitwise operation: Right arithmetic shift
Right arithmetic shift
Bitwise operation: Right logical shift
Right logical shift

Worked examples

Example 1 — a first encounter with Bitwise operation

Start with the simplest possible case. Write down what Bitwise operation claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bitwise operation before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bitwise operation ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bitwise operation

In research
Bitwise operation appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bitwise operation in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bitwise operation is common in secondary-school and first-year university syllabi. It links to neighbouring topics Binary arithmetic, Boolean algebra, Operators (programming), so understanding it makes those chapters shorter.
In everyday life
Look for Bitwise operation outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bitwise operation” →

How to study Bitwise operation in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bitwise operation means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bitwise operation out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bitwise operation in simple terms?

In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor.

Why does Bitwise operation matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bitwise operation?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bitwise operation.

Tags

  • Binary arithmetic
  • Boolean algebra
  • Operators (programming)

Keep exploring