22. Bit Masks and Flags
Select individual bits with AND and set them with OR.
Open this lesson →
1. Predict and trace
Before running the Try program, predict its output and trace the first three executed instructions. Track the relevant registers, flags or memory as needed. Then step the program to compare.
LOAD 13
AND 5
PRINT
OR 2
PRINT
HALT
Queue input: None
Initialize memory: All bytes initially zero
Try prediction key
Output: 5, 7
2. Build and check
Read a byte from 0 through 255. Print only its bits 0 and 2 as a numeric mask, using AND 5.
Required instruction types: INPUT, AND
Example challenge solution
INPUT
AND 5
PRINT
HALT
Actual checker fixtures
Case 1
- Input
7- Initial memory
All bytes initially zero- Expected output
5
Case 2
- Input
10- Initial memory
All bytes initially zero- Expected output
0
Case 3
- Input
20- Initial memory
All bytes initially zero- Expected output
4
Case 4
- Input
255- Initial memory
All bytes initially zero- Expected output
5
3. Explain the machine
What does the mask5 select?
Reasoning and teaching note
Binary00000101 selects bits 0/2, clearing other bits. It does not numerically subtract other values.
23. Toggling Bits
Use XOR’s reversible change to update a saved setting.
Open this lesson →
1. Predict and trace
Before running the Try program, predict its output and trace the first three executed instructions. Track the relevant registers, flags or memory as needed. Then step the program to compare.
LOAD 42
XOR 6
PRINT
XOR 6
PRINT
HALT
Queue input: None
Initialize memory: All bytes initially zero
Try prediction key
Output: 44, 42
2. Build and check
Read one byte. Toggle bits 1 and 2, store the toggled byte at address 20, and print it after reading it back. Toggle those bits again, save the restored byte at address 20, and print the restored value.
Required instruction types: INPUT, XOR, STORE, LOAD
Example challenge solution
INPUT
XOR 6
STORE 20
LOAD [20]
PRINT
XOR 6
STORE 20
LOAD [20]
PRINT
HALT
Actual checker fixtures
Case 1
- Input
0- Initial memory
All bytes initially zero- Expected output
6, 0- Expected final memory
20: 0- Required memory reads
20
Case 2
- Input
42- Initial memory
All bytes initially zero- Expected output
44, 42- Expected final memory
20: 42- Required memory reads
20
Case 3
- Input
255- Initial memory
All bytes initially zero- Expected output
249, 255- Expected final memory
20: 255- Required memory reads
20
3. Explain the machine
What happens after toggling the same XOR mask twice?
Reasoning and teaching note
Original value returns:42 XOR 6=44, then44 XOR 6=42.
24. Signed Bits and the Largest Value
Understand NOT and test a signed difference with a sign mask.
Open this lesson →
1. Predict and trace
Before running the Try program, predict its output and trace the first three executed instructions. Track the relevant registers, flags or memory as needed. Then step the program to compare.
LOAD 255
NOT
PRINT
NOT
PRINT
HALT
Queue input: None
Initialize memory: All bytes initially zero
Try prediction key
Output: -256, 255
2. Build and check
Read two integers, each from −10,000 through 10,000, and print the larger value. Use a subtraction and the signed 32-bit sign mask.
Required instruction types: INPUT, SUB, AND, JUMP_IF_ZERO
Example challenge solution
INPUT
COPY R0 R1
INPUT
COPY R0 R2
LOAD R1
SUB R2
AND -2147483648
JUMP_IF_ZERO first
LOAD R2
JUMP show
first: LOAD R1
show: PRINT
HALT
Actual checker fixtures
Case 1
- Input
5, 9- Initial memory
All bytes initially zero- Expected output
9
Case 2
- Input
9, 5- Initial memory
All bytes initially zero- Expected output
9
Case 3
- Input
7, 7- Initial memory
All bytes initially zero- Expected output
7
Case 4
- Input
-3, 2- Initial memory
All bytes initially zero- Expected output
2
Case 5
- Input
-8, -2- Initial memory
All bytes initially zero- Expected output
-2
3. Explain the machine
Why is NOT 255 equal to−256 rather than0?
Reasoning and teaching note
NOT inverts all signed32 bits. A byte-only complement would require a separate mask255.