NAND Bricks

How to build a full adder from NAND gates

A full adder can be built from nine NAND gates. Four make A XOR B, four more XOR that with the carry in to give the sum, and one last NAND combines two of the earlier gates into the carry out. It adds three bits: A, B and the carry from the bit below.

Inputs
1NAND2NAND3NAND4NAND5NAND6NAND7NAND8NAND9NANDA1B0Cin10Sum1Cout
Click the inputs to try every combination. Green wires carry 1 and red wires carry 0. The small numbers match the steps below.
Open this circuit in NAND BricksLoads it as a new board with its truth table, ready to run.

The formula

Sum = A XOR B XOR Cin, Cout = (A AND B) OR (Cin AND (A XOR B))

How it works

Each column of a multi-bit addition has three bits to add: one from each number and the carry from the column to its right. Three bits add up to at most 3, which is 11 in binary, so you get a sum bit and a carry out.

The sum is 1 when an odd number of inputs are 1, which is two XORs in a row: (A XOR B) XOR Cin. Gates 1 to 4 are the first XOR and gates 5 to 8 are the second.

The carry out is 1 when A and B are both 1, or when exactly one of them is 1 and the carry in is 1. Both of those facts are already sitting inside the circuit: gate 1 is NAND(A, B) and gate 5 is NAND(A XOR B, Cin). One more NAND of those two gives the OR of the two cases, which is the carry out. So the carry costs just one gate.

Gate by gate

  1. NAND(A, B). Starts the first XOR, and later feeds the carry.
  2. NAND(A, n1). Part of the first XOR.
  3. NAND(n1, B). Part of the first XOR.
  4. A XOR B.
  5. NAND(A XOR B, Cin). Starts the second XOR, and later feeds the carry.
  6. NAND(n4, n5). Part of the second XOR.
  7. NAND(n5, Cin). Part of the second XOR.
  8. The sum: A XOR B XOR Cin.
  9. The carry out: NAND(n1, n5), which is (A AND B) OR ((A XOR B) AND Cin).

Truth table

ABCinSumCout
00000
00110
01010
01101
10010
10101
11001
11111

Why 9 NAND gates?

Nine NAND gates is the classic full adder, and the one most courses teach. Building it as two separate half adders plus an OR would take 5 + 5 + 3 = 13 gates, so sharing gates 1 and 5 with the carry saves four.

Where it's used

Chain full adders and you can add numbers of any width. The carry out of each one becomes the carry in of the next. The 4-bit adder guide does exactly that.