A computer is a pile of one kind of switch wired together in different ways. This lesson shows that a single gate, NAND, is enough to build every other gate, and then every logic function you can write as a table. This lesson pairs with Exploring How Computers Work by Sebastian Lague, linked under Watch & read. The video walks from logic gates to binary numbers, an adder and an ALU in a simulator. Here you build NOT, AND, OR, XOR, multiplexers and a generator for any truth table out of one function, nand, and the tests count how many NAND calls each circuit uses.
A bit is 0 or 1. NAND returns 0 only when both inputs are 1: NAND(a, b) = NOT (a AND b). Tie its two inputs together and you have an inverter, because NAND(a, a) is 1 when a is 0 and 0 when a is 1. AND is a NAND followed by that inverter. OR needs the inputs inverted first: NAND(NOT a, NOT b) is NOT (NOT a AND NOT b), which equals a OR b. That is De Morgan's law written as a circuit.
Gates cost something, so count them. NOT is 1 NAND, AND is 2, OR is 3. XOR (1 when exactly one input is 1) can be done in 4, if you compute NAND(a, b) once and feed it to two more gates.
nand.def nand(a, b):
return 0 if (a and b) else 1
def xor(a, b):
shared = nand(a, b)
return nand(nand(a, shared), nand(b, shared))
print([xor(a, b) for a in (0, 1) for b in (0, 1)])A multiplexer chooses between two inputs. With a select bit sel it returns a when sel is 0 and b when sel is 1: NAND(NAND(a, NOT sel), NAND(b, sel)). When sel is 0 the right gate outputs 1, so the last gate returns NOT (NOT a), which is a. Chain multiplexers in layers and one select bit per layer halves the choices, so 2ᵏ words need k select bits and 2ᵏ - 1 multiplexers per bit of word width. A demultiplexer does the reverse: it sends one input to one of two outputs and leaves the other at 0.
Now the general result. Any truth table can be built from AND, OR and NOT, with no cleverness. For every row whose output is 1, make a term that is 1 for exactly that input: the input bit itself where the row has a 1, the inverted bit where it has a 0. AND the literals together, then OR all the terms. This is called a sum of products.
calls = 0
def nand(a, b):
global calls
calls += 1
return 0 if (a and b) else 1
def not_(a): return nand(a, a)
def and_(a, b): return not_(nand(a, b))
def or_(a, b): return nand(not_(a), not_(b))
def xor_terms(a, b):
return or_(and_(not_(a), b), and_(a, not_(b)))
print([xor_terms(a, b) for a in (0, 1) for b in (0, 1)], calls // 4)The terms version gives the right table but spends 9 NAND gates per evaluation where the shared design spends 4. A sum of products always works and is rarely the cheapest circuit. Hardware designers spend much of their effort on that gap.
Two mistakes are common. The first is swapping the select wiring of a multiplexer, so sel = 0 picks b. It still produces a legal bit on every input, only the wrong one half the time, and it uses the same number of gates, so the call count will not warn you. The second is leaving a negation out of one literal in a term. Terms that need no negation, such as a AND b, hide the mistake, and a table that happens to include only those rows passes. Test the rows that need inverted inputs, and the last input in particular.
A third habit to avoid is using Python's &, | or ^ inside a gate. The answers come out right and the circuit is not a circuit. That is why every test reads counter.calls as well as the output.
How do you know a circuit is right? A circuit with n inputs has only 2ⁿ input rows, so you can try them all and compare with Python's operators. That is a proof for the function, not a sample. It does not tell you how fast the circuit is, how many gates a better design would use, or whether the table you were given describes the thing you meant to build. And it stops scaling quickly: a circuit with two 64-bit inputs has 2¹²⁸ rows. Larger circuits are checked by building them from parts that were each checked this way, which is where the next lesson starts.