← All lessons

Bits and Representation · Lesson 23 of 30 · about 12 minutes

Toggling Bits

Use XOR’s reversible change to update a saved setting.

01 / Explain

Understand the idea

XOR flips a bit when the mask bit is one and preserves it when the mask bit is zero. XOR 6 toggles bits 1 and 2. Applying the same mask twice restores the original value.

A reversible operation can be tested with an invariant: after two toggles, the saved setting equals the starting setting. Memory makes this visible even when R0 has changed during the program.

02 / Try

Watch it happen

Expect 44 then 42. Compare the changed and restored bits.

LOAD 42
XOR 6
PRINT
XOR 6
PRINT
HALT

Use Step to follow one instruction at a time. You can change the example and replay it.

03 / Challenge

Make it work

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.

The checker runs your current editor program in a fresh machine for each of 3 test cases. It supplies inputs and seeded memory itself; the lab’s current output and memory do not decide your result.

INPUT
XOR 6
STORE 20
LOAD [20]
PRINT
# Toggle again, save and print the restored value.
HALT
Need a hint?

Use XOR 6 both times; the final memory byte must equal the original input.

Reveal a worked solution

Read the program, predict each instruction’s effect, then step through it in the lab.

INPUT
XOR 6
STORE 20
LOAD [20]
PRINT
XOR 6
STORE 20
LOAD [20]
PRINT
HALT

Emoji CPU lab

Ready · 0 cycles

Emoji program

Type LOAD, ADD, or another opcode then Space to insert emoji. Ctrl/⌘ + Enter runs or pauses; Escape pauses; Ctrl/⌘ + ] indents. Tab moves focus. Labels use a colon. Jumps use zero-based instruction addresses.

Instruction map and breakpoints (0)

Breakpoints stop before an instruction. Run resumes past the stopped breakpoint once; Step executes it directly. Editing source clears old breakpoints and machine state.

    CPU registers

    R0
    0
    R1
    0
    R2
    0
    R3
    0
    R4
    0
    R5
    0
    R6
    0
    R7
    0
    Program counter (PC)
    0
    Stack pointer (SP)
    255
    Flags
    Zero: off · Negative: off · Overflow: off
    Delay request
    0 ms
    Stacks and loop frames

    SP = 255 − data depth − call depth. The stack is separate from memory.

    Data stack (bottom → top)

    Empty

    Call return addresses (bottom → top)

    Empty

    Loop frames

    Empty

    Output and input

    Run a PRINT instruction to see output.

    Queued input: Empty

    Memory · 256 bytes · 0 nonzero

    Each cell shows address:value. R = read this step; W = written this step. Select a cell to inspect or initialize it before execution. Use arrow keys to move, Home/End for the row, and Ctrl/⌘ + Home/End for the whole memory.

    Reset before initializing memory. Stored values wrap to 0–255.
    Execution trace · 0 entries

    Recent entries below. Inspect any zero-based index to see complete detached before/after state.

    Check your challenge

    You can run this check any time. Every case must pass to record completion.

    Progress uses localStorage only. It stays on this browser and is never sent to a server.