Bit masking
Score 0 / 0
Streak 0
Best 0
Show the working, column by column
Press Enter for the next question
How bit masks work
A mask is a binary number that is combined with another number, bit by bit, using a Boolean operation. Each column is worked out on its own.
| A | B | A AND B | A OR B | A XOR B |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
- AND to clear bits or keep only some bits: a 0 in the mask makes the bit 0, a 1 in the mask keeps the bit as it was.
- OR to set bits: a 1 in the mask makes the bit 1, a 0 in the mask keeps the bit as it was.
- XOR to flip (toggle) bits: a 1 in the mask flips the bit, a 0 in the mask keeps the bit as it was.
For example, to check whether the bit worth 8 is on, AND the number with Binary 0000 1000. The result is 0 if the bit is off.