Condition Flags and Comparisons
To compare and , the processor computes , throws the answer away, and keeps four flag bits: (negative), (zero), (carry) and (overflow). Every comparison, signed or unsigned, is a small logic formula on those four bits: signed less-than is , and unsigned less-than is "no carry", a borrow.
What the symbols mean
- Negative flag
The top bit of the result.
- Zero flag
1 when every result bit is 0.
- Carry flag
The carry out of A + NOT B + 1: 1 means no borrow.
- Overflow flag
1 when the signed subtraction overflowed.
- Top result bit
The sign bit of r.
Explanation
Condition flags are simply four single-bit notes the ALU writes down about its last result: was it negative (), was it zero (), did a carry come out of the top (), did signed overflow happen (). A compare instruction is just a subtraction whose answer is thrown away, keeping only those notes, and a branch like "jump if less" reads a small formula of them. The formulas look like alphabet soup, but each flag is one wire or one gate, and you've met all four already: and in the overflow lesson, the subtraction in the last-but-one lesson. This is also where the question from the start of the course gets its final answer: the processor never decides whether is negative. The branch instruction you choose, signed or unsigned, decides which flag formula to read.
Why subtract? says everything about how compares with : it's zero exactly when they're equal, and its sign says which is bigger, as long as the subtraction didn't overflow. The ALU computes it as , and those same bits and carries are valid under both readings; only the interpretation of the flags differs.
Unsigned: the carry means "no borrow". From the subtraction lesson, the adder's full result is , which reaches (a carry out) exactly when . So for unsigned numbers means , and exactly when . (x86 stores the opposite bit after a subtraction and calls it the borrow, so there means . Same information, flipped.)
Signed: . If the subtraction didn't overflow (), the result is the true , so exactly when the result is negative, . If it did overflow (), the result wrapped by and its sign is backwards, so exactly when . Put the two cases together: exactly when , which is . For example, and in 8 bits: wraps to , so , but , and correctly says .
Every comparison from four bits.
| condition | signed | unsigned |
|---|---|---|
| (that is, ) | ||
The two columns are why every instruction set has two families of branches. On x86, "less" and "greater" (jl, jg) are signed while "below" and "above" (jb, ja) are unsigned; ARM has LT/GT and LO/HI. Same flags, two readings.
Watch one pair of bytes get both answers. Compare with . The ALU computes , with , , (, no borrow) and ( fits). Unsigned less-than reads : no, isn't less than . Signed less-than reads : yes, . There's an animation further down that runs one against every possible .
RISC-V skips flags entirely: slt/sltu and blt/bltu compute the comparison straight into a register or a branch. Inside, it's the same subtraction and the same formulas.
A common mistake. It's tempting to decide signed less-than from alone: "the difference is negative, so is smaller." That's right only when the subtraction didn't overflow. , yet in 8 bits wraps to and . is there exactly to flip the verdict in that case.
See it
Worked examples
An 8-bit processor compares 0x90 with 0x30. Find , , and , then decide signed and unsigned less-than.
- 1
Write the operands: 0x90 , which is unsigned and signed; 0x30 either way.
A C program has int x = -1; unsigned n = 5; and tests if (x < n). C converts to unsigned before comparing. On a 32-bit machine, what does the branch decide, and why?
- 1
C's rule: when a signed and an unsigned
intmeet, the signed one is converted to unsigned. The bits of are all 1s, so as unsigned becomes .
Practice
Type your answer and check it. Solve them all to complete the lesson.
An 8-bit processor compares with . What are , , and ? Enter them in that order.
An 8-bit processor compares 0x7F with 0xFF. What are , , and ? Enter them in that order.
Which flag condition means signed after computing ?
x86's ja jumps when is "above" , an unsigned . Using the convention where means no borrow, which flags say that?
A thermostat stores temperatures as signed bytes. Its firmware checks "is ?" by computing and testing only the flag (a bug). At , what value does the subtraction leave in the register, and what is ? Enter both.
With fixed, for how many of the possible bytes do the signed and unsigned "" verdicts disagree?
On 32-bit RISC-V, slt writes 1 if as signed numbers and sltu writes 1 if as unsigned. For and , what do slt and sltu write? Enter both.
The hardest one here. Get it, and you've got this lesson.
Flipping the top bit of both operands turns a signed comparison into an unsigned one. Use it on 8-bit and : give the two flipped bytes as unsigned numbers, then the carry flag from comparing them unsigned (computing ). Enter all three.
Where it's used
Every if, loop bound and sort comparison in compiled code becomes a compare plus a conditional branch on these flags, and choosing the signed or unsigned branch is where integer types finally matter. Classic security bugs come from that choice: a negative length that passes a signed bounds check and then gets used as a huge unsigned size.