Working with Bitsets: Setting and Testing Individual Bits
Understanding why the result is all zeros is more useful than memorizing it. The same reasoning lets you predict any AND of a bitset with a related bitset, because you only need to ask at each position whether both bits can be 1.
Why Predictable Bit Rules Matter
A bitset is a collection of bits that holds a fixed number of positions, and you can change or read the whole collection with a single expression. That convenience hides a risk. If you do not know exactly what each operator does to each position, a result that looks plausible can be wrong in ways that are hard to spot.
This article focuses on two families of operators. The bitwise operators combine or invert bits, one position at a time. The shift operators move every bit a given number of positions to the left or to the right. Once you can predict both families, you can read most bitset results without stepping through them by hand.
Reading a Bitset Position by Position
Think of a bitset as a row of slots. Each slot holds one bit, and every slot has a position. The left end of the bitset holds the bits that carry the most weight, and the right end holds the bits that carry the least. When you write a bitset as a pattern such as 1011, the leftmost character is the most significant position and the rightmost character is the least significant position.
The width of a bitset is the number of positions it holds. The width is fixed. That fixed width matters for every operation in this article, because a result is always read back into the same set of positions. Bits cannot grow beyond the edge, and the operators do not invent new positions to hold extra bits.
A bitset is a fixed-width collection of bits. Operators on a bitset either act on the whole set at once, as the complement operator does, or combine two sets position by position, as the AND operator does.
Complement Flips Every Bit
The complement operator ~ applies to a bitset as a whole and flips every bit. Each 0 becomes 1, and each 1 becomes 0. You do not need to look at positions one at a time to know the outcome, because the rule is the same at every position.
Complementing a Four-Bit Pattern
A four-bit bitset holds 1011. What is the value after applying the complement operator?
- 1Read the original positions
From left to right the positions hold 1, 0, 1, 1.
- 2Flip each position
The 1 becomes 0, the 0 becomes 1, the next 1 becomes 0, and the last 1 becomes 0.
- 3Read the new pattern
The complemented bitset holds 0100.
The complement of 1011 is 0100 in a four-bit bitset.
What do you think happens?
What is the complement of 0110 in a four-bit bitset?
Reveal answer
1001
Each position flips: 0 becomes 1, 1 becomes 0, 1 becomes 0, and 0 becomes 1.
Combining Bitsets with AND, OR, and XOR
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. Other bitwise operators, such as OR and XOR, follow the same position-by-position pattern with their own rules. The source states that the usual bitwise operations can be applied to bitsets in this way.
| Operator | Result for 1100 and 1010 | Rule at each position |
|---|---|---|
| AND (&) | 1000 | 1 only when both bits are 1 |
| OR (|) | 1110 | 1 when at least one bit is 1 |
| XOR (^) | 0110 | 1 only when the two bits differ |
The same two input bitsets combined with three different bitwise operators. Only the rule at each position changes.
Notice that the operator is the only thing that changed between the three rows. The inputs and the position-by-position reading stayed the same. When you choose an operator, you are choosing the rule that every position will follow, so pick the rule first and then read each position against it.
Why AND With a Complement Gives All Zeros
The source example makes a useful point about a bitset combined with its own complement. At every position, the complement holds the opposite of the original bit. An AND of a bit with its own opposite is always 0, because the two bits can never both be 1. So when a bitset is combined with its complement, every position of the result is 0, and the result is all zeros.
Understanding why the result is all zeros is more useful than memorizing it. The same reasoning lets you predict any AND of a bitset with a related bitset, because you only need to ask at each position whether both bits can be 1.
Reasoning About AND With a Complement
A four-bit bitset holds 0110. What is 0110 AND its complement?
- 1Find the complement
The complement of 0110 is 1001.
- 2Check each position
Position 3 combines 0 and 1, which gives 0. Position 2 combines 1 and 0, which gives 0. Position 1 combines 1 and 0, which gives 0. Position 0 combines 0 and 1, which gives 0.
- 3Compare with the rule
No position has both bits equal to 1, so no position can be 1.
The result is 0000, all zeros, as the rule predicts.
Shifting Bits Left and Right
The shift operators move every bit of a bitset a given number of positions. The number after the operator sets how far the bits move. The shift operators shift in zeros, so the positions that open up on the vacated side are filled with 0. Bits that move past an edge of the fixed-width bitset are discarded rather than wrapped around to the other end.
Where Results Diverge From Expectations
A result diverges from what you might expect when you forget that the width is fixed. Because bits shifted past the edge are discarded, a shift never wraps bits around to the other end. Shifting 1011 left by one gives 0110 in a four-bit bitset, not 10110. The leftmost 1 is lost, and the result still has only four positions.
The complement is also bounded by the width. Complementing 1011 in a four-bit bitset gives 0100. The result never includes positions beyond the width of the set, so the complement describes exactly the positions the bitset owns and nothing more.
Mistakes Beginners Make With Bitsets
Common mistake Treating a shift as a rotation
What it looks likeShifting 1011 left by one and expecting 0110 plus a wrapped 1 at the right end.
Why it failsBits that pass an edge are discarded, and the vacated side is filled with 0. Nothing wraps around.
Do this insteadCompute the shifted pattern, then drop anything that passes the edge and fill the opened side with zeros.
Common mistake Filling the vacated positions with the shifted-out bits
What it looks likeShifting 1011 right by one and writing 1101 because the rightmost 1 seemed to come back on the left.
Why it failsThe shift operators shift in zeros, so the opened left end is 0.
Do this insteadWrite 0 in each opened position, then place the remaining bits next to them.
Common mistake Forgetting that the width is fixed
What it looks likeReading the shifted result of 1011 by one as 10110.
Why it failsThe bitset has four positions, so the result must still have four positions.
Do this insteadKeep the number of positions equal to the width after every operation.
Common mistake Reading the wrong operator's rule
What it looks likeUsing the OR rule when the expression uses AND, so 1100 AND 1010 is read as 1110.
Why it failsEach operator has its own rule at every position, and the result depends on which rule applies.
Do this insteadName the operator first, then apply its rule at each position.
Common mistake Expecting a bitset combined with its complement to keep some 1s, when every position is actually 0
Tracing a Bitset Sequence
The source includes a short trace of bitset operations on a nine-bit bitset. Before you read the explanations for it, work through the trace line by line. For each line, write down the bit pattern you expect, then compare it with what the trace shows. Pay close attention to the direction the bits travel during a shift and to the value that fills the positions the shift opens up.
A four-bit bitset holds 1100. Predict the result of each operation below, then check your answers: (a) complement of 1100, (b) 1100 shifted left by one, (c) 1100 shifted right by two, (d) 1100 AND 0011.
Hints
- For the complement, flip each of the four positions.
- For a shift, move the bits, drop anything past the edge, and fill the opened side with 0.
- For the AND with 0011, check each position: 1 only where both bits are 1.
Key Takeaways
- The complement operator ~ flips every bit of a bitset, and the other bitwise operators combine bitsets one position at a time.
- AND returns 1 only where both input bits are 1, so an AND of a bitset with its own complement is all zeros.
- The shift operators << and >> move bits toward the left or right end and shift in zeros on the vacated side.
- Bits shifted past the edge of a fixed-width bitset are discarded, so a shift never wraps bits around.
- When a result looks wrong, check the width, the direction of the shift, and the operator used.