A-Level Computer Science / Unit 4: CPU Operation and Low-Level Processing

4.3.2 Bit Masking and Device Monitoring/Control

🔒 Lesson slides are available to signed-in users. Sign in

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.

Flag: a bit used to represent a yes/no state. In this page, 1 means the state is on or true, and 0 means it is off or false.

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 positionb7b6b5b4b3b2b1b0
Example single-bit mask10000000

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 startAND maskAND resultMeaning at the selected bit
101001011000000010000000The tested flag is 1, so it is on.
101001010100000000000000The 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.

Explain both parts: state the AND result, then say what that result shows about the selected flag.

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 bitXOR mask bitNew target bitEffect
011The flag is set.
110The flag is cleared.
0 or 10UnchangedOther 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.

BIT MASK LAB

Test, set or toggle a flag

8-bit byte · b0 is rightmost
Current byte
10100101
Operation

Selected bit: b7

Mask10000000
Operation result—

Choose a bit and apply AND to check its flag.

A test reports the flag state but leaves the current byte unchanged. OR and XOR update the byte.

6. Check your understanding

Answer each prompt before opening the suggested response.

  1. 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.

  2. 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.

  3. 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.

  4. 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.