Bitwise Operators on Integers
The complement operator ~ flips every bit of a bitset, and the other bitwise operators such as & combine bitsets one position at a time.
Predicting Bits Without Stepping Through Them
A bitset is a fixed number of bits, and bitwise operators let you act on the whole set in a single expression. Reading a line like that and knowing the answer is a useful skill, because it lets you understand bitset code without tracing each bit by hand. This article teaches the two families of operators behind those expressions. The first family combines or inverts bits, one position at a time. The second family, the shift operators, moves every bit a given number of positions to the left or to the right.
What do you think happens?
A four-bit bitset holds 1011. What does the complement operator ~ produce? Write down your guess before reading on.
Reveal answer
0100
The complement flips every bit: each 1 becomes 0 and each 0 becomes 1. Within four positions, 1011 becomes 0100.
Two Families of Bit Operators
The complement operator ~ applies to a bitset as a whole and flips every bit. Each 0 becomes 1 and each 1 becomes 0. It works on the whole set in one step rather than one position at a time.
The AND operator & combines two bitsets position by position. At each position, the result bit is 1 only when both input bits at that position are 1. If either input bit is 0, the result bit is 0.
Other bitwise operators, such as OR and XOR, follow the same position-by-position pattern with their own rules. OR gives a 1 when at least one input bit is 1, and XOR gives a 1 when the two input bits differ. Once you know that the operation is applied to each position separately, you only need the per-bit rule for each operator to predict the whole result.
Reading Results Position by Position
The clearest way to read a bitwise result is to go through the positions from left to right and apply the per-bit rule at each one. The diagram below does this for an AND of two four-bit values, 1011 and 1110. Each position is treated separately, and the input bits at that position are shown beneath the result bit.
Reading the diagram from left to right, the first position has 1 and 1, so the result is 1. The second position has 0 and 1, so the result is 0, because at least one input is 0. The third position has 1 and 1, giving 1. The last position has 1 and 0, giving 0. The full result is 1010.
Why AND With a Complement Gives Zeros
Consider what happens when a bitset is combined with its own complement. At every position, the two bits are opposites, so one of them is always 0. An AND of a bit with its own opposite is therefore always 0, because the two bits can never both be 1. This means every position of the result is 0, and the whole result is all zeros.
Combining 1011 with its complement
Let bs1 hold 1011 in a four-bit bitset. Compute bs1 AND ~bs1.
- 1Find the complement
Flip every bit of 1011. The result is 0100.
- 2Pair the bits position by position
Position 3 pairs 1 with 0, position 2 pairs 0 with 1, position 1 pairs 1 with 0, and position 0 pairs 1 with 0.
- 3Apply AND at each position
Every pair has one 0 and one 1, so every AND result is 0.
- 4Read the final bitset
All four positions are 0, so the result is 0000.
0000
The reasoning matters more than the memorized outcome. The same position-by-position check tells you the result of any AND between a bitset and a related bitset, not only the case where the second bitset is the complement of the first.
Shifting Bits Toward One End
The shift operators move every bit of a bitset a given number of positions in one direction. The number written after the operator sets how far the bits travel. The two directions are handled by two operators, and both follow the same rule about what fills the gap they leave behind. The subsections below take each direction in turn.
Fixed Width Decides What Survives
A bitset has a fixed width, meaning it holds a set number of positions. That width decides which bits exist at all. A complement is applied across every position the bitset has, so the same value can produce different bit patterns at different widths. The diagram below shows the complement of the four-bit value 1011 written out in an eight-bit bitset, where the value sits in the lower four positions and the upper four are 0 before the complement.
In a four-bit bitset, the complement of 1011 is 0100. In an eight-bit bitset holding the same value, the complement is 11110100. The two results are not the same bit pattern, and that difference is one of the most common reasons a bitset result looks wrong. Shifts follow the same width rule: a bit shifted past the left edge is discarded rather than wrapping around to the right end.
Comparing the Operators on Two Bitsets
Different operators combine the same bits in different ways. The table applies several operators to the same two four-bit values, 1011 and 1110. The complement is applied to 1011 alone, and the shifts are applied to 1011 alone as well. Comparing the rows shows how the operator decides the outcome at each position.
| Operator | Operands | Result (four bits) | Rule applied |
|---|---|---|---|
| ~ | 1011 | 0100 | Flip every bit |
| & | 1011 and 1110 | 1010 | 1 only where both bits are 1 |
| | | 1011 and 1110 | 1111 | 1 where at least one bit is 1 |
| ^ | 1011 and 1110 | 0101 | 1 where the two bits differ |
| << | 1011 by 1 | 0110 | Move left, discard the leftmost bit, shift in 0 |
| >> | 1011 by 1 | 0101 | Move right, discard the rightmost bit, shift in 0 |
Six bitwise operators applied to the same four-bit values
Diagnosing a Result That Looks Wrong
When a bitset result differs from what you expected, the cause is almost always one of three things: the width, the direction of the shift, or the operator. Checking them in a fixed order helps you find the mismatch quickly. Start with the width, because it decides which bits exist. Then check the shift direction, because it decides which end the bits leave from and which end the zeros enter. Finally, check the operator, because it decides the rule applied at each position.
- 1Confirm the width of the bitset, and write every bit position including leading zeros.
- 2Confirm the shift direction: left moves bits toward the high-weight end, right moves them toward the low-weight end.
- 3Confirm the operator, and apply its per-bit rule at each position rather than guessing the whole result.
- 4Recompute one position at a time and compare with the observed bit pattern to find the first position that differs.
Mistakes Beginners Make with Bitsets
Common mistake Assuming a shifted-out bit wraps around to the other end
What it looks likeShifting 1011 left by one and expecting 0111 instead of 0110.
Why it failsA bit that passes the edge of a fixed-width bitset is discarded, and the bitset has no room for it.
Do this insteadDiscard the bit that leaves the edge and place a 0 in the vacated position.
Common mistake Filling the vacated position with 1
What it looks likeShifting 1011 right by one and writing 1101 because a 1 was placed on the left.
Why it failsThe shift operators shift in zeros, so the vacated position is always 0.
Do this insteadWrite a 0 in the position the shift opens up, on the left for >> and on the right for <<.
Common mistake Ignoring leading zeros when computing a complement
What it looks likeTaking the complement of 1011 as 0100 when the bitset is eight bits wide and should give 11110100.
Why it failsThe complement flips every position in the bitset, including positions that hold leading zeros.
Do this insteadWrite out every position of the bitset before flipping, then flip all of them.
Common mistake Expecting AND of a value with its complement to give the value back
What it looks likeBelieving 1011 AND ~1011 gives 1011.
Why it failsEach position pairs a bit with its opposite, and an AND of opposite bits is always 0.
Do this insteadRecognize the pattern: the result is all zeros for any bitset combined with its own complement.
Practice Predicting Bitset Results
A four-bit bitset holds 1100. Predict the result of 1100 AND ~1100, then predict the result of 1100 << 1 and 1100 >> 1. For each one, write the bit pattern before checking. If your result differs, identify whether the width, the shift direction, or the operator caused the difference.
Hints
- Flip every bit of 1100 first when working with ~.
- The AND of any bit with its opposite is 0.
- For the shifts, bits leave one edge and 0 enters the opposite edge.
Key Takeaways
- The complement operator ~ flips every bit of a bitset, and AND, OR, and XOR combine bits position by position using their own per-bit rules.
- An AND of a bit with its own opposite is always 0, so a bitset combined with its complement gives all zeros.
- The shift operators << and >> move bits toward one end and shift in zeros on the vacated side, and bits that pass the edge are discarded without wrapping.
- A bitset has a fixed width, so the same value can give different bit patterns at different widths, and width is a common cause of unexpected results.
- When a result looks wrong, check the width, then the shift direction, then the operator, in that order.