Teacher answer key

Bits and Representation

Gather four kinds of evidence: prediction of relevant state; behavior across varied cases including boundaries; explanation using trace/state evidence; and a reasoned correction of one mismatch. Mark each as emerging, with support or independent using paper or your school’s existing approved process. A challenge pass is machine-behavior evidence, not proof of authorship or complete mastery. Accept equivalent correct programs; the public key is a model, not the only possible answer.

Public teaching material. Solutions are examples; equivalent correct programs may also pass the actual checks.

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

InstructionPC beforePC afterR0 beforeR0 afterRelevant stateOutput
LOAD01013{"registers":[13,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[],"memoryReads":[],"memoryWrites":[]}None
AND12135{"registers":[5,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[],"memoryReads":[],"memoryWrites":[]}None
PRINT2355{"registers":[5,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[],"memoryReads":[],"memoryWrites":[]}5

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.