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

4.3.2 Bitwise Logic and Masks

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

4.3.2 Bitwise Logic and Masks

Bitwise operations compare corresponding bits in two fixed-width patterns. A mask is a deliberately chosen bit pattern that allows selected positions to be tested or changed while the remaining positions are ignored or preserved.

By the end of this section, you should be able to:

  • Apply bitwise AND, OR and XOR to two 8-bit values.
  • Explain that each pair of corresponding bits is processed independently.
  • Use immediate and memory-based operands with AND, OR and XOR instructions.
  • Construct masks that test, set, clear or toggle a chosen bit.
  • Use a mask to isolate several selected bits.
  • Distinguish testing a bit from changing a bit.
  • Interpret the result of an AND mask by comparing it with zero or with the mask.
  • Write short assembly-language fragments that load, manipulate and store a byte.

Where This Page Fits

Page Main focus
4.3.1 Binary Shifts and Their Effects Moving bits left or right.
4.3.2 Bitwise Logic and Masks AND, OR, XOR, and masks for testing or changing selected positions.
4.3.3 Using Bits to Read and Control Devices Applying masks to sensor flags, output registers and control systems.
Structural note: The uploaded old webpage explains control systems, actuators, ADC, DAC and feedback. That useful material belongs in 4.3.3 and is being preserved for that page. It is not duplicated here because this lesson focuses on the bit-level operations that device-control code will later use.

Bitwise Operations Work Position by Position

The left operand is normally the current value in ACC. The second operand may be an immediate binary value or a byte loaded from memory. Bit 7 is paired with bit 7, bit 6 with bit 6, and so on.

ACC 10100110
Mask 00011100
Apply one rule to every pair AND / OR / XOR
Input A Input B A AND B A OR B A XOR B
00000
01011
10011
11110
Bitwise operation: an operation that applies a Boolean rule separately to each pair of corresponding bits.
Common misconception: Bitwise AND is not the same as the Boolean operator joining two complete conditions. Here, eight independent one-bit AND operations produce an 8-bit result.

AND: Keep a Bit Only Where Both Inputs Contain 1

AND rule: the result bit is 1 only when both corresponding input bits are 1.
ACC10100110 AND00111100 Result00100100

A 1 in an AND mask allows the original ACC bit to pass through. A 0 forces the result bit to zero. This makes AND suitable for:

  • isolating one or more bits;
  • testing whether selected bits are set;
  • clearing selected bits with a carefully designed mask.
AND mask behaviour ACC bit AND 1 → original bit preserved ACC bit AND 0 → result forced to 0

OR: Force Selected Bits to 1

OR rule: the result bit is 1 when either or both corresponding input bits are 1.
ACC10100110 OR00010001 Result10110111

A 1 in an OR mask forces the matching result bit to 1. A 0 leaves the original ACC bit unchanged.

OR mask behaviour ACC bit OR 1 → result forced to 1 ACC bit OR 0 → original bit preserved

OR is therefore the standard operation for setting one or more bits without disturbing the rest of the byte.

XOR: Toggle Selected Bits

XOR rule: the result bit is 1 when the two input bits are different.
ACC10100110 XOR00100011 Result10000101

A 1 in an XOR mask reverses the matching ACC bit. A 0 leaves it unchanged.

XOR mask behaviour ACC bit XOR 1 → bit toggled ACC bit XOR 0 → original bit preserved

Applying the same XOR mask twice restores the original value:

10100110 XOR 00100011 = 10000101
10000101 XOR 00100011 = 10100110

Assembly Instruction Forms

The syllabus teaching instruction set allows each bitwise operation to use either an immediate operand or the contents of a memory address.

Form Second operand Example
AND #n / Bn / &n An immediate denary, binary or hexadecimal mask. AND B00000100
AND <address> The byte stored at the given absolute or symbolic address. AND TEST_MASK
OR #n / Bn / &n An immediate mask. OR &10
OR <address> A mask stored in memory. OR ENABLE_MASK
XOR #n / Bn / &n An immediate mask. XOR B00000010
XOR <address> A mask stored in memory. XOR TOGGLE_MASK

These notations represent the same 8-bit mask:

#16 B00010000 &10
Exam tip: Include the number-base marker when the instruction syntax requires it. A binary mask should be written with B, and a hexadecimal mask with &.

Designing a One-Bit Mask

Start by numbering the positions from bit 7 on the left to bit 0 on the right. For a single-bit mask, place a 1 at the target position and 0 everywhere else.

Target bit Single-1 mask Hexadecimal form
710000000&80
601000000&40
500100000&20
400010000&10
300001000&08
200000100&04
100000010&02
000000001&01
Bit mask: a bit pattern chosen so that a bitwise operation affects or reveals particular bit positions.

Testing Whether a Bit Is Set

To test bit 5, AND the value with a mask containing 1 only at bit 5:

Value10100110 AND00100000 Result00100000

The result is not zero, so bit 5 was set. If bit 5 had contained 0, the result would have been 00000000.

LDD FLAGS
AND B00100000
CMP #0
JPN BIT5_SET

In this instruction set, JPN follows the comparison and jumps when the comparison with zero is false. That means the masked result was not zero.

Two valid testing ideas:
  • Compare the masked result with zero to ask, “Is any selected bit set?”
  • Compare the masked result with the complete mask to ask, “Are all selected bits set?”
Common mistake: Testing a bit with AND changes the value currently held in ACC. Reload the original byte later if the complete unmasked value is still needed.

Setting, Clearing and Toggling a Bit

Task Operation Mask design Example using bit 4
Set to 1 OR 1 at the target; 0 elsewhere OR B00010000
Clear to 0 AND 0 at the target; 1 elsewhere AND B11101111
Toggle XOR 1 at the target; 0 elsewhere XOR B00010000

Set bit 4

10100110 OR  00010000 = 10110110

Clear bit 5

10100110 AND 11011111 = 10000110

Toggle bit 1

10100110 XOR 00000010 = 10100100
Mask pattern warning: A clear mask is the opposite of a test/set mask. It contains 0 at the bit to clear and 1 in every position that must remain unchanged.

Working with Several Bits at Once

A mask may select more than one position. Suppose a settings byte contains 11010101 and bits 6, 3 and 0 are of interest:

Settings11010101 AND01001001 Selected bits01000001

The result is not equal to zero, so at least one selected bit is set. It is not equal to the mask 01001001, so not all three selected bits are set.

Question Operation Comparison
Is at least one selected bit set? value AND mask Result is not zero
Are all selected bits set? value AND mask Result equals the mask
Set several bits value OR mask No comparison required
Toggle several bits value XOR mask No comparison required

Interactive: Bitwise Mask Builder

Choose a preset task or enter your own ACC value, operation and mask. Step across the eight bit positions to see how the result is formed.

ACC
AND
Result
Current bit 7
Calculation 1 AND 0 = 0
Mask effect This bit is forced to zero.

Bit 7 of 8

Apply AND to every corresponding pair

The animation separates the positions for learning, although the processor applies the same bitwise rule across the complete word.

Complete result 00100100
AND keeps only positions where ACC and mask both contain 1.

Common Mistakes and Misconceptions

  • Applying one operation to the whole denary value instead of pairing corresponding binary bits.
  • Numbering the rightmost position as bit 7 rather than bit 0.
  • Using OR when testing a bit. OR may set the bit and change the value.
  • Using a single-1 mask with AND to clear a bit; this preserves only the target instead.
  • Forgetting that XOR with 1 toggles while XOR with 0 preserves.
  • Assuming a non-zero test result proves that all selected bits were set.
  • Storing a masked test result over the original byte when the original data is still required.
  • Omitting the load–modify–store sequence when a changed byte must be written back to memory.

Exam Tips

Write bit positions above the byte

Label the positions 7 6 5 4 3 2 1 0 before constructing the mask.

Explain why unaffected bits stay unchanged

For OR and XOR, use 0 in positions to preserve. For an AND clear mask, use 1 in positions to preserve.

State what the comparison proves

“Not zero” means at least one selected bit is set. “Equal to the mask” means every selected bit is set.

Practice

Calculate the results

  1. 11001010 AND 01110100
  2. 10010001 OR 00101100
  3. 11100011 XOR 01010110
  4. 00111100 AND 11110000
  5. 01010101 XOR 11111111

Construct the masks

  1. Test bit 6.
  2. Set bit 3.
  3. Clear bit 2 while preserving every other bit.
  4. Toggle bits 7 and 0.
  5. Isolate bits 5, 4 and 1.

Write assembly fragments

  1. Load OPTIONS, set bit 3, and store the modified byte.
  2. Load FLAGS, test bit 6, and jump to READY when it is set.
  3. Load MODE, toggle bits 2 and 1, and store the result.
  4. Use the mask stored at CHECK_MASK to isolate selected ACC bits.

Review

Task Operation Mask at target bit Mask elsewhere
Test / isolateAND10
SetOR10
ClearAND01
ToggleXOR10
Final check: Can you construct the correct 8-bit mask, apply the operation, and explain exactly what the result proves or changes?