Bit Masks and Device States
A program can store several on/off states in one byte. With a bit mask, it can check or change one selected bit while leaving other bits as they are. This is useful when a processor monitors inputs or controls outputs.
By the end of this page, you should be able to:
- use an AND mask to test a selected bit;
- use an OR mask to set a selected bit to 1;
- use an XOR mask to toggle a selected bit;
- explain how flags represent device states.
1. Read the bit positions
In an 8-bit byte, positions are numbered from b0 on the right to b7 on the left. Each position can store one flag, such as whether a sensor has detected a condition or whether an output is on.
A mask is another bit pattern with the same width as the byte. Line up the mask and byte so that each bit is combined with the bit in the same position. To focus on one position, put 1 in that position of the mask.
| Bit position | b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 |
|---|---|---|---|---|---|---|---|---|
| Example single-bit mask | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
The example mask selects b7. The zeros in the other positions mean those positions are not the target.
2. AND masks test a bit
To find out whether a particular flag is set, combine the byte with an AND mask that has 1 only at the selected position. AND returns 1 only when both bits in that position are 1.
| Byte at start | AND mask | AND result | Meaning at the selected bit |
|---|---|---|---|
| 10100101 | 10000000 | 10000000 | The tested flag is 1, so it is on. |
| 10100101 | 01000000 | 00000000 | The tested flag is 0, so it is off. |
The AND result is used as evidence. A non-zero result means the selected bit was 1; an all-zero result means it was 0. Testing does not need to change the stored byte.
3. OR masks set a bit
To set a flag to 1, use an OR mask with 1 in the target position and 0 everywhere else. OR produces 1 if either input bit is 1. Therefore, the target becomes 1 and the other bits stay as they were.
Original byte: 10100101
Mask to set b6: 01000000
After OR: 11100101
This operation sets b3 whether it was 0 or already 1. It does not mean “switch the bit”: OR with 1 always leaves the target at 1.
4. XOR masks toggle a bit
XOR with 1 changes a bit to its opposite: 0 becomes 1, and 1 becomes 0. XOR with 0 leaves a bit unchanged. Use a mask with 1 only at the position to toggle.
| Current target bit | XOR mask bit | New target bit | Effect |
|---|---|---|---|
| 0 | 1 | 1 | The flag is set. |
| 1 | 1 | 0 | The flag is cleared. |
| 0 or 1 | 0 | Unchanged | Other flags are preserved. |
Original byte: 10100101
Toggle b7: 10100101 XOR 10000000 = 00100101
b7 was 1, so the toggle changes it to 0.
XOR does not always clear a flag
XOR toggles the selected bit. It clears that bit only if the bit was 1 before the operation. If it was 0, XOR changes it to 1.
5. Use flags to monitor and control a device
A program can pack several device states into one byte. For example, different bits could represent whether separate inputs are active or whether separate outputs are enabled. The bit positions need a clear meaning agreed by the program that writes the byte and the program that reads it.
- Monitor: use AND to check an input flag without changing the byte.
- Set an output: use OR to make the selected output flag 1.
- Toggle an output: use XOR to switch the selected output flag to its opposite state.
When explaining a result, name the bit position and what that flag means. Then state whether the flag is on or off after the operation. This links the binary result to the device behaviour.
Interactive: operate on one flag
Select a bit position and an operation. The lab displays the mask and result, then explains what happened. Try each operation with the starting byte and reset before exploring another sequence.
6. Check your understanding
Answer each prompt before opening the suggested response.
- What does a non-zero result from an AND test tell you about the selected bit?
Suggested response
The selected bit was 1, so the flag is set or on.
- Which operation sets a selected bit to 1 while preserving the other bits?
Suggested response
OR with a mask that contains 1 at the target position and 0 in the other positions.
- A selected bit is 0 before XOR with a mask bit of 1. What is its new value?
Suggested response
It becomes 1. XOR toggles the bit, so the effect depends on its starting value.
- Give one way a program can use a byte of flags when working with a device.
Suggested response
It can use AND to check a sensor flag, OR to set an output flag, or XOR to toggle an output flag.