← All lessons

Bits and Representation · Lesson 24 of 30 · about 15 minutes

Signed Bits and the Largest Value

Understand NOT and test a signed difference with a sign mask.

01 / Explain

Understand the idea

NOT inverts all 32 bits, not just a byte: NOT 255 produces −256. AND −2147483648 selects the signed 32-bit sign bit. On a small difference, a nonzero sign mask means the difference is negative.

To choose the larger of a and b, compute a − b and test its sign. If the difference is negative, choose b; otherwise choose a. Restrict this method to the documented small input range, because 32-bit coercion can lose information for large arithmetic differences.

02 / Try

Watch it happen

Expect −256 and then 255. The negative flag is a state display, not a separate conditional-negative opcode.

LOAD 255
NOT
PRINT
NOT
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 two integers, each from −10,000 through 10,000, and print the larger value. Use a subtraction and the signed 32-bit sign mask.

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

INPUT
COPY R0 R1
INPUT
COPY R0 R2
LOAD R1
SUB R2
AND -2147483648
# Choose a register value, then print.
HALT
Need a hint?

A zero sign mask means a − b is nonnegative, so a is at least b.

Reveal a worked solution

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

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

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.