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. |
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.
10100110
00011100
AND / OR / XOR
| Input A | Input 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: Keep a Bit Only Where Both Inputs Contain 1
10100110
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.
ACC bit AND 1 → original bit preserved
ACC bit AND 0 → result forced to 0
OR: Force Selected Bits to 1
10100110
OR00010001
Result10110111
A 1 in an OR mask forces the matching result bit to 1. A 0 leaves the original ACC bit unchanged.
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
10100110
XOR00100011
Result10000101
A 1 in an XOR mask reverses the matching ACC bit. A 0 leaves it unchanged.
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
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 |
|---|---|---|
| 7 | 10000000 | &80 |
| 6 | 01000000 | &40 |
| 5 | 00100000 | &20 |
| 4 | 00010000 | &10 |
| 3 | 00001000 | &08 |
| 2 | 00000100 | &04 |
| 1 | 00000010 | &02 |
| 0 | 00000001 | &01 |
Testing Whether a Bit Is Set
To test bit 5, AND the value with a mask containing 1 only at bit 5:
10100110
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.
- 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?”
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
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:
11010101
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.
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
11001010 AND 0111010010010001 OR 0010110011100011 XOR 0101011000111100 AND 1111000001010101 XOR 11111111
Construct the masks
- Test bit 6.
- Set bit 3.
- Clear bit 2 while preserving every other bit.
- Toggle bits 7 and 0.
- Isolate bits 5, 4 and 1.
Write assembly fragments
- Load
OPTIONS, set bit 3, and store the modified byte. - Load
FLAGS, test bit 6, and jump toREADYwhen it is set. - Load
MODE, toggle bits 2 and 1, and store the result. - Use the mask stored at
CHECK_MASKto isolate selected ACC bits.
Review
| Task | Operation | Mask at target bit | Mask elsewhere |
|---|---|---|---|
| Test / isolate | AND | 1 | 0 |
| Set | OR | 1 | 0 |
| Clear | AND | 0 | 1 |
| Toggle | XOR | 1 | 0 |