C7 · Lesson 18 · The arithmetic logic unit
One block that adds, subtracts, ANDs and ORs, and the four flags that let a program compare
Lesson 17 built adders. A processor needs more than addition from the same hardware: subtraction, comparison, the logical operations, all selected by a few control bits from the instruction. That block is the arithmetic logic unit, and alongside its result it reports four facts about it: was the result zero, was it negative, did an unsigned operation carry out, did a signed one overflow? Those four flags are how every comparison in a program is made, and getting them right is a small, exact exercise in two's complement.
- A 2-bit ALU built from two slices.
Op1 Op0selects the operation: 00 AND, 01 OR, 10 ADD, 11 SUBTRACT. Flip the inputs and watch the result and flags. - Subtraction is addition with inverted and a carry of 1 into the lowest slice: .
- Try = 01, = 01 with SUBTRACT: the result is 00, lights, and = 1 because no borrow was needed.
Predict
A 4-bit ALU computes 5 − 7 by adding 0101, 1010 and a carry-in of 1. What are the result and the carry out?
, which is −2 in two's complement, with no carry out of the top bit. In this convention (used by RISC-V comparisons, ARM and the 6502), a carry of 0 after a subtraction means a borrow happened: unsigned 5 is less than unsigned 7.
Name it
Bit slices and function select
An -bit ALU is copies of a one-bit slice: a full adder, the logic gates, and a multiplexer that picks the slice's output according to the operation code. The adder's carries ripple (or look ahead) from slice to slice, and subtraction reuses them: XOR gates invert when subtracting, and the lowest carry-in is set to 1, because in two's complement.
The four flags
After an operation producing result with carries out of the top bit and into it:
| Flag | Meaning | Logic |
|---|---|---|
| Z, zero | NOR of all result bits | |
| N, negative | 's sign bit | |
| C, carry | unsigned carry out; after a subtraction, 1 means no borrow | |
| V, overflow | the signed result didn't fit |
Overflow can be detected two ways that always agree: the carries into and out of the sign bit differ, or two operands of the same sign produced a result of the other sign (lesson 17).
Comparisons from flags
Compute and discard the result:
- : Z = 1.
- Unsigned : C = 0 (a borrow).
- Signed : . The sign bit alone is wrong when the subtraction overflowed, and the XOR corrects it.
That's what a "compare" instruction does, and what conditional branches test. RISC-V skips the flags register and computes the comparison directly in the branch instruction, but the logic inside is the same.
Speed
The ALU's critical path is the carry chain: in a ripple design, about two gate delays per bit, plus the multiplexer, plus the zero detector, which is a tree of levels of 2-input gates. Lookahead adders shorten the chain to a few levels, which is why fast ALUs use them.
Worked example
A 4-bit ALU subtracts B = 0111 (7) from A = 0101 (5). Find the result and all four flags, and say what the flags conclude about A < B, signed and unsigned.
Show the solution
- Add bit by bit.
| Bit | Sum | Carry out |
|---|---|---|
| 0 | 0 | 1 |
| 1 | 1 | 0 |
| 2 | 1 | 0 |
| 3 | 1 | 0 |
- Read the result. The result is 1110 = −2.
- Set the flags. and , so C = 0 and V = 0; N = 1 and Z = 0.
- Unsigned comparison. C = 0 means a borrow, so .
- Signed comparison. , so again.
- Why they agree. Both agree here because neither number has its top bit set.
Answer the result is 1110 (−2), with C = 0, V = 0, N = 1 and Z = 0; both comparisons say A < B.
Worked example
Now subtract B = 0111 (+7) from A = 1000 (−8 signed, 8 unsigned). Find the flags and the comparisons.
Show the solution
- Add bit by bit. Only bit 3 carries: carry 1.
| Bit | Sum | Carry out |
|---|---|---|
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 2 | 0 | 0 |
| 3 | 0 | 1 |
- Set the flags. The result is 0001 with a carry out: and , so C = 1, V = 1, N = 0 and Z = 0.
- Unsigned comparison. C = 1, no borrow, so : right.
- The signed result. The result 0001 = +1 is wrong, since −8 − 7 = −15 doesn't fit in 4 bits, and V flags it.
- Signed comparison. says : right.
- Why N alone fails. Testing N alone would have said .
Answer C = 1, V = 1, N = 0 and Z = 0. Unsigned, C = 1 says ; signed, says . Both are right.
Worked example
A 32-bit ripple-carry ALU uses gates of 20 ps. The carry takes two gate delays per bit, the output multiplexer two more, and the zero flag a tree of 2-input NORs and ANDs after that. How long until Z is valid? What does a 4-input gate tree save?
Show the solution
- Carry chain.
- Multiplexer. Two more gate delays: 40 ps.
- Zero detector. It combines 32 bits in = 5 levels: 100 ps.
- Total.
- With 4-input gates. The tree takes = 3 levels, 60 ps, saving 40 ps, about 3%.
- Where the time goes. The carry chain is 90% of the delay, which is the case for lookahead.
Answer Z is valid after 1420 ps; a 4-input gate tree saves 40 ps, about 3%.
Check
A 4-bit ALU computes A − B for A = 0101 and B = 0001. What 4-bit result does it produce?
Check
A 4-bit ALU subtracts B = 0001 from A = 1010. Signed, A is negative and B positive. What result does it produce? (The V flag will say whether it's wrong.)
Check
After computing A − B, write the signed less-than condition from the N and V flags, using AND, OR and NOT.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
Write the zero flag of a 4-bit result.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
In the play's ALU, Op1 Op0 = 11 means subtract. Write the slice's adder input , which is B for every other operation and when subtracting.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
A 16-bit ripple ALU has gates of 10 ps: two gate delays per bit of carry, two for the output multiplexer, then a zero detector built as a tree of 2-input gates. How long until Z is valid?
Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.
Check
Computing A − B on 8 bits sets C = 1, N = 1, V = 1, Z = 0. Which is true?
Check
Write alu(a, b, op) for an n-bit ALU (16 bits), with op one of 'and', 'or', 'add', 'sub', taking unsigned inputs below and returning (r, z, neg, c, v): the result and the four flags as 0 or 1, computed the way hardware does (subtract as ; C is the carry out of the top bit; V from the carries into and out of the top bit; logic operations clear C and V). Then write signed_lt(a, b) and unsigned_lt(a, b) using only the flags from alu(a, b, 'sub').
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
A programmer tests whether a signed 8-bit temperature is below a threshold by subtracting and checking only the sign of the result. It works in testing but fails for a reading of −100 against a threshold of 50. Explain using the ALU's flags.
The true difference, −100 − 50 = −150, doesn't fit in 8 bits, whose signed range is −128 to 127. The ALU's subtraction wraps around to +106, so the sign bit N is 0 and the test says −100 is not below 50. The ALU also sets the overflow flag V, because the carries into and out of the sign bit differ, which means the sign bit is wrong. The correct signed less-than test is , which here is 1, and that's what a signed comparison or a signed branch instruction checks. The bug only shows when the operands are far apart with opposite signs, which is why ordinary test values missed it.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.