NAND Bricks

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.

Inputs
HalfAdderABSumCarryFullAdderABCinSumCoutFullAdderABCinSumCoutFullAdderABCinSumCoutA11A21A30A40B11B20B31B400S10S20S31S40Cout
Shown adding 3 + 5. A1 and B1 are the lowest bits. Green wires carry 1 and red wires carry 0.
Open this circuit in NAND BricksLoads it as a new board with its truth table, ready to run.

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

  1. A half adder adds A1 and B1, the lowest bits. Its sum is S1 and its carry goes to the next stage.
  2. A full adder adds A2, B2 and the carry from bit 1.
  3. A full adder adds A3, B3 and the carry from bit 2.
  4. 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.

A1A2A3A4B1B2B3B4S1S2S3S4Cout
0000000000000
1000100001000
1100101000010
1110100000010
0110011000110
1001001010110
0001000100001
1111111101111

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.