Invert the basics
Figure: Gate symbols for NAND, NOR and XOR side by side: the AND and OR shapes each with a small inversion bubble on the output
Add an inversion bubble to a gate's output and you get its compound twin:
- NAND: AND then NOT. Output is 0 only when all inputs are 1.
- NOR: OR then NOT. Output is 1 only when all inputs are 0.
- XOR (exclusive OR): output is 1 when the inputs differ.
The little bubble on a symbol always means "invert this". Spotting it tells you half of what a gate does at a glance.
Their truth table
The circuit above feeds inputs a and b into a NAND, a NOR and an XOR gate at once. Toggle them and check each output against the table below.
| A | B | NAND | NOR | XOR |
|---|
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 0 |
Compare the NAND column with AND from the last lesson: every row is flipped. NOR does the same to OR. XOR is the odd one out, a "these two disagree" detector, and it turns up all over binary arithmetic.
One gate to build them all
NAND is universal: any logic function at all, however large, can be built from NAND gates alone.
Figure: Three small schematics built only from NAND gates, each labelled with its gate count: NOT as one NAND with both inputs tied together
- NOT = one NAND with both inputs tied to the same signal.
- AND = two NANDs: the second inverts the first.
- OR = three NANDs: invert both inputs, then NAND the results.
Once you can build NOT, AND and OR, you can build anything, so NAND alone is enough for an entire chip. NOR is universal too, in mirror image.
NAND in the wild
A NAND gate is the cheapest useful logic that CMOS can make: a handful of transistors, fast and compact. So the industry perfected it and built much of everything else from it.
- Early processors were built almost entirely from NAND.
- NAND flash is named after it: the SSD in your laptop is billions of NAND based cells storing charge.
- When a synthesis tool later turns a design into silicon, this is the kind of small, perfected cell it maps onto.