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.
- 9NAND gates
- 3inputs
- 2outputs
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
- NAND(A, B). Starts the first XOR, and later feeds the carry.
- NAND(A, n1). Part of the first XOR.
- NAND(n1, B). Part of the first XOR.
- A XOR B.
- NAND(A XOR B, Cin). Starts the second XOR, and later feeds the carry.
- NAND(n4, n5). Part of the second XOR.
- NAND(n5, Cin). Part of the second XOR.
- The sum: A XOR B XOR Cin.
- The carry out: NAND(n1, n5), which is (A AND B) OR ((A XOR B) AND Cin).
Truth table
| A | B | Cin | Sum | Cout |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
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.