Skip to content

Level 7 · Chapter 7.1

Gates and Boolean algebra

How two voltages become a logic: gates and their truth tables, the 16 functions of two inputs, Boolean algebra and De Morgan's laws, building any function from its truth table, simplifying circuits, and why NAND alone is enough — all on live circuits.

Every instruction the CPU runs — every add, cmp and and from the assembly level — ends up as signals flowing through gates: tiny circuits that take a few bits in and produce one bit out. A modern processor has billions of them. This level builds everything from gates: adders and the ALU, multiplexers and decoders, then memory and registers.

From voltages to bits

A digital circuit uses just two voltage ranges. Near 0 volts means 0; near the supply voltage — around 1 volt in a modern CPU — means 1. Anything in between is forbidden. This gap is what makes digital logic robust: a signal that has picked up some noise is still clearly 0 or 1, and each gate outputs clean levels again.

A gate computes a fixed function of its inputs. How gates are built from transistors belongs to the device level below. At this level, a gate is just a function, described by its truth table: one row per combination of inputs.

The basic gates

Toggle A and B and watch every gate respond at once. The truth table under the circuit highlights the current row:

Logic · The basic gates
auto
0
gate delays
stable after 0
stable
state
1
critical path
gate delays, worst case
7
gates
ABNOT gate: output 11NOT AAND gate: output 00A AND BOR gate: output 00A OR BXOR gate: output 00A XOR BNAND gate: output 11A NAND BNOR gate: output 11A NOR BXNOR gate: output 11A XNOR B
1 0 inputs changed, output switches next delayclick a switch to toggle it
ABNOT AA AND BA OR BA XOR BA NAND BA NOR BA XNOR B
001000111
011011100
100011100
110110001

Every basic gate fed by the same two switches. Toggle A and B and watch the truth table follow. NAND and NOR are universal: every other gate can be built from either one.

Unit-delay model: every gate takes one step to react. With auto off, toggle switches and press step to watch the change travel gate by gate.

GateOutput is 1 when…Symbol in algebra
NOTthe input is 0A̅ (or ¬A)
ANDboth inputs are 1A·B, or AB
ORat least one input is 1A + B
XORexactly one input is 1A ⊕ B
NANDnot both are 1¬(AB)
NORneither is 1¬(A + B)
XNORthe inputs are equal¬(A ⊕ B)

The small circle on NOT, NAND, NOR and XNOR is an inversion bubble: it means "and then invert".

How many functions are there?

A function of n inputs is completely described by its truth table, which has 2ⁿ rows, each with an output of 0 or 1. So there are 2^(2ⁿ) possible functions: 16 functions of two inputs. List the rows in the order 00, 01, 10, 11, and each function has a 4-bit name made of its output column: AND is 0001, OR is 0111, XOR is 0110, NAND is 1110, NOR is 1000. The other eleven include the constants 0000 and 1111, "just A", "just B", and a few like "A and not B".

With three inputs there are 256 functions, and with four there are 65,536. Truth tables grow quickly, which is why we also need an algebra.

Boolean algebra

Boolean algebra, named after George Boole (1815–1864), is algebra with variables that are only 0 or 1. AND works like multiplication, OR like addition (except that 1 + 1 = 1), and NOT is written with a bar (A̅), or ¬ in front of a group. Most rules look familiar; a few don't:

LawAND formOR form
identity1·A = A0 + A = A
null0·A = 01 + A = 1
idempotentA·A = AA + A = A
inverseA·A̅ = 0A + A̅ = 1
commutativeAB = BAA + B = B + A
associative(AB)C = A(BC)(A + B) + C = A + (B + C)
distributiveA(B + C) = AB + ACA + BC = (A + B)(A + C)
absorptionA(A + B) = AA + AB = A
De Morgan¬(AB) = A̅ + B̅¬(A + B) = A̅·B̅

Two rules have no equivalent in school algebra: idempotence (A + A = A) and the second distributive law (OR distributes over AND). De Morgan's laws are the ones you'll use most: to invert an AND, invert the inputs and use an OR, and vice versa. That's also true in code: !(a && b) is !a || !b.

From a truth table to a circuit

Any function can be built directly from its truth table:

  1. For every row whose output is 1, write the AND of the inputs, inverting the ones that are 0 in that row. That's a product term.
  2. OR all the product terms together.

The result is a sum of products. Take the majority function of three inputs, which is 1 when at least two inputs are 1. It outputs 1 on the rows 011, 101, 110 and 111, so:

M = A̅BC + AB̅C + ABC̅ + ABC

Logic · Majority, straight from the truth table
auto
0
gate delays
stable after 0
stable
state
3
critical path
gate delays, worst case
8
gates
ABCNOT gate: output 1NOT gate: output 1NOT gate: output 1AND gate: output 0AND gate: output 0AND gate: output 0AND gate: output 0OR gate: output 00M
1 0 inputs changed, output switches next delayclick a switch to toggle it
ABCM
0000
0010
0100
0111
1000
1011
1101
1111

M is 1 when at least two inputs are 1. Written straight from the truth table: one AND gate per row that outputs 1 (A'BC, AB'C, ABC', ABC), all ORed together. It works for any function, but it's rarely the smallest circuit.

Unit-delay model: every gate takes one step to react. With auto off, toggle switches and press step to watch the change travel gate by gate.

Three inverters, one 3-input AND per product term, and a 4-input OR. The method always works, but it rarely gives the smallest circuit.

Equivalent circuits

Boolean algebra can shrink it. Because ABC = ABC + ABC + ABC (idempotence), the term ABC can be paired with each of the other three:

  • A̅BC + ABC = BC(A̅ + A) = BC
  • AB̅C + ABC = AC
  • ABC̅ + ABC = AB

so M = AB + AC + BC:

Logic · Majority, simplified
auto
0
gate delays
stable after 0
stable
state
2
critical path
gate delays, worst case
4
gates
ABCAND gate: output 0AND gate: output 0AND gate: output 0OR gate: output 00M
1 0 inputs changed, output switches next delayclick a switch to toggle it
ABCM
0000
0010
0100
0111
1000
1011
1101
1111

The same function as AB + AC + BC: three 2-input ANDs and one OR instead of three NOTs, four 3-input ANDs and a 4-input OR. Boolean algebra proves the two circuits equivalent; compare their truth tables.

Unit-delay model: every gate takes one step to react. With auto off, toggle switches and press step to watch the change travel gate by gate.

It has the same truth table, but uses three 2-input ANDs and one 3-input OR, instead of three inverters, four 3-input ANDs and a 4-input OR. Fewer gates means less area, less power, and a shorter path for the signal. Chip design tools do this kind of simplification automatically, on millions of gates.

One gate is enough

NAND is universal: you can build NOT, AND and OR — and therefore any function — from NAND gates alone.

Logic · NOT, AND and OR from NAND gates
auto
0
gate delays
stable after 0
stable
state
2
critical path
gate delays, worst case
6
gates
ABNAND gate: output 11NOT ANAND gate: output 1NAND gate: output 00A AND BNAND gate: output 1NAND gate: output 1NAND gate: output 00A OR B
1 0 inputs changed, output switches next delayclick a switch to toggle it
ABNOT AA AND BA OR B
00100
01101
10001
11011

NAND is universal: tie both inputs together and it's NOT; follow it with a NOT and it's AND; feed it two inverted inputs and it's OR (De Morgan). So any circuit can be built from NAND gates alone — and NOR works the same way.

Unit-delay model: every gate takes one step to react. With auto off, toggle switches and press step to watch the change travel gate by gate.

  • NOT: a NAND with both inputs tied together: ¬(AA) = A̅.
  • AND: a NAND followed by that NOT.
  • OR: by De Morgan, A + B = ¬(A̅·B̅), which is a NAND of the two inverted inputs.

NOR is universal too, by the mirror-image argument. Among the two-input gates, only NAND and NOR have this property. They're also the natural gates of CMOS, the technology of every modern chip. A CMOS NAND or NOR needs 4 transistors, while AND and OR need 6, because they're a NAND or NOR followed by an inverter. So hardware often thinks in NANDs and NORs, even when the designer wrote AND and OR.

Gates in your code

These gates are exactly what C's bitwise operators and the x86 logic instructions compute — 64 of them side by side, one per bit:

Cx86Gate, per bit
a & bandAND
a | borOR
a ^ bxorXOR
~anotNOT

The logical operators &&, || and ! are different. They treat a whole value as true or false and, for && and ||, skip evaluating the right side when they can, so they usually compile to conditional jumps rather than to a single logic instruction.

Takeaways

  • A gate computes one bit from a few input bits. Its truth table lists the output for every input combination.
  • Two inputs allow 16 functions; n inputs allow 2^(2ⁿ).
  • Boolean algebra — especially De Morgan's laws — rewrites circuits without changing what they compute.
  • Any function can be built as a sum of products from its truth table, then simplified into an equivalent circuit with fewer gates.
  • NAND (and NOR) alone can build everything, and they're the natural gates of CMOS.

In this level

  1. 7.1Gates and Boolean algebra
  2. 7.2Adders, the ALU and the flags
  3. 7.3Multiplexers, decoders and buses
  4. 7.4Latches, flip-flops and clocks
  5. 7.5Registers and memory arrays
  6. 7.6SRAM, DRAM, ROM and flash chipsPlanned
  7. 7.7CPU chips, pins and packagesPlanned
  8. 7.8Bus timing, handshakes and arbitrationPlanned
  9. 7.9Real buses: PCI, PCI Express and USBPlanned
  10. 7.10I/O chips and address decodingPlanned