Logic Gates & Combinational Circuits
How transistors become gates, and gates become adders, multiplexers, decoders and the ALU — circuits whose output depends only on their current input.
Everything a CPU computes is done by combinational circuits: networks of logic gates whose outputs depend only on their current inputs (no memory). This lesson builds up from transistors to a working ALU.
From transistors to gates
Modern chips use CMOS: pairs of complementary transistors (nMOS and pMOS). A NOT gate (inverter) is just two transistors. A NAND gate is four. Because NAND (or NOR) is functionally complete, any Boolean function can be built from it alone.
| Gate | Symbol | Output is 1 when… | Transistors (CMOS) |
|---|---|---|---|
| NOT | ¬A | A is 0 | 2 |
| NAND | ¬(A·B) | not both inputs are 1 | 4 |
| NOR | ¬(A+B) | both inputs are 0 | 4 |
| AND | A·B | both are 1 | 6 (NAND + NOT) |
| OR | A+B | at least one is 1 | 6 |
| XOR | A⊕B | inputs differ | 8–12 |
From truth table to circuit
Any truth table can be turned into a circuit with sum of products: OR together one AND term for each row whose output is 1. Then simplify with Boolean algebra or a Karnaugh map to use fewer gates.
Example — the majority function (1 if at least two of A, B, C are 1):
Adders
A half adder adds two bits: sum = A ⊕ B, carry = A · B. A full adder also takes a carry-in:
A ripple-carry adder chains n full adders. It's simple, but the carry has to ripple through all n stages, so the delay is O(n). Real CPUs use carry-lookahead (or prefix adders such as Kogge–Stone), which compute all the carries in parallel in O(log n) gate delays.
Subtraction reuses the adder: A − B = A + (¬B) + 1 — invert B and set the carry-in to 1 (two's complement).
Multiplexers and decoders
- A multiplexer (MUX) picks one of 2ⁿ inputs using n select bits: "if s then b else a" in hardware. CPUs are full of them: choosing between a register value and an immediate value, choosing which result to write back.
- A decoder turns an n-bit number into 2ⁿ outputs with exactly one active. Used to select a memory row or a register.
- An encoder does the reverse; a priority encoder reports the highest active input (for example, which interrupt to handle first).
The ALU
The arithmetic logic unit computes every operation in parallel (add, subtract, AND, OR, XOR, shift, compare) and a MUX selects the requested result using the operation code. It also produces flags:
| Flag | Meaning | Used for |
|---|---|---|
| Z (zero) | Result is 0 | if (a == b) → subtract and test Z |
| N (negative) | Top bit is 1 | Signed comparisons |
| C (carry) | Unsigned overflow out of the top bit | Unsigned comparisons, multi-word arithmetic |
| V (overflow) | Signed overflow | Detecting signed overflow |
Multiplication and shifting
- Shifts are just wiring: a barrel shifter built from layers of MUXes shifts by any amount in one cycle.
- Multiplication is shift-and-add, done in hardware with parallel adder trees (Wallace trees). It typically takes 3–4 cycles. Division is much slower (20–40+ cycles), which is why compilers replace division by a constant with a multiply and shift.
- Combinational circuits: output depends only on current input.
- CMOS transistors → NAND/NOR → any Boolean function.
- Full adders chain into ripple-carry adders; carry-lookahead makes addition O(log n) deep.
- MUXes select, decoders activate one line; both are everywhere in a CPU.
- The ALU computes all operations and a MUX picks one; flags record zero, sign, carry and overflow.
QWhy can NAND build any circuit?medium
NOT A = A NAND A. AND = NOT(A NAND B). OR = (NOT A) NAND (NOT B) by De Morgan. Since AND, OR and NOT can express any truth table, so can NAND alone.
QWhy is integer division slow compared to addition?medium
Addition can be computed in parallel with carry-lookahead in a few gate levels. Division is inherently sequential: each quotient bit depends on the remainder from the previous step, so it takes many cycles.