How to build a 4-bit adder from NAND gates
A 4-bit adder takes 32 NAND gates: a half adder (5 gates) for the lowest bit and three full adders (9 gates each) for the rest, each passing its carry to the next. It adds two numbers from 0 to 15 and gives a 4-bit sum plus a carry out for results up to 30.
- 32NAND gates
- 8inputs
- 5outputs
The formula
S = A + B, with the carry rippling from bit 1 up to bit 4
How it works
This is long addition, done the way you learned it on paper but in base 2. Add the lowest column, write the sum, carry into the next column, and repeat.
The lowest bit has no carry coming in, so a half adder is enough. Every bit above it needs a full adder, which takes the carry from the bit below as its third input.
This design is called a ripple-carry adder because the carry ripples up one bit at a time. That makes it simple but slow for wide numbers, since the top bit has to wait for every carry below it. Real CPUs use carry-lookahead tricks to speed this up, but ripple carry is where everyone starts.
In NAND Bricks, the half adder and full adders are saved as bricks. Switch to Masonry view to see all 32 NAND gates light up as the carry moves, or double-click a brick to look inside it.
Brick by brick
- A half adder adds A1 and B1, the lowest bits. Its sum is S1 and its carry goes to the next stage.
- A full adder adds A2, B2 and the carry from bit 1.
- A full adder adds A3, B3 and the carry from bit 2.
- A full adder adds A4, B4 and the carry from bit 3. Its carry out is the fifth output bit.
Truth table
With 8 inputs there are 256 rows, so here are a few examples. A1 and S1 are the lowest bits, so read each number right to left: A = A4 A3 A2 A1.
| A1 | A2 | A3 | A4 | B1 | B2 | B3 | B4 | S1 | S2 | S3 | S4 | Cout |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| 1 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 1 | 0 |
| 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 1 |
Why 32 NAND gates?
5 + 9 + 9 + 9 = 32 NAND gates. Using full adders for every bit, with the bottom carry in held at 0, would cost 36, so starting with a half adder saves four.
Where it's used
An adder is the heart of an ALU. With an XOR on each B input you can also subtract (two's complement), and the same carry chain drives counters and address arithmetic.