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
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
Starter program
INPUT
# Keep bits 0 and 2.
PRINT
HALT
Your program and notes
3. Explain the machine
What does the mask5 select?
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
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
Starter program
INPUT
XOR 6
STORE 20
LOAD [20]
PRINT
# Toggle again, save and print the restored value.
HALT
Your program and notes
3. Explain the machine
What happens after toggling the same XOR mask twice?
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
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
Starter program
INPUT
COPY R0 R1
INPUT
COPY R0 R2
LOAD R1
SUB R2
AND -2147483648
# Choose a register value, then print.
HALT
Your program and notes
3. Explain the machine
Why is NOT 255 equal to−256 rather than0?