Notebooks · posts/building-a-four-bit-adder-out-of-nand-gates
Building a four-bit adder out of NAND gates
A NAND gate wires into NOT, AND, OR and XOR. Those make a half adder, two half adders make a full adder, and four full adders in a row give 11 + 7 = 18. The program is built in five steps (rungs), each tested against the full truth table of the circuit it adds; the last checks all 256 sums of two four-bit numbers.
Bits here are the numbers 0 and 1, a gate is a function of its inputs, and a wire is a variable holding a bit. That models a circuit at the level of logic gates, above transistors and below an instruction set.
The last rung draws three pictures: the wiring of one column (one full adder), a timing diagram of that column as its three inputs count from 000 to 111, and the sum 11 + 7, where the carry has to pass through all four columns, one full-adder carry delay (two gate levels) each. That delay is what makes this design slow, and it is what a carry-lookahead adder removes.
Look inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.