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.

Beginner 4 min readLesson 1 of 15 Interview Q&A

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.

GateSymbolOutput is 1 when…Transistors (CMOS)
NOT¬AA is 02
NAND¬(A·B)not both inputs are 14
NOR¬(A+B)both inputs are 04
ANDA·Bboth are 16 (NAND + NOT)
ORA+Bat least one is 16
XORA⊕Binputs differ8–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):

M=AˉBC+ABˉC+ABCˉ+ABC=AB+BC+ACM = \bar{A}BC + A\bar{B}C + AB\bar{C} + ABC = AB + BC + AC

Adders

A half adder adds two bits: sum = A ⊕ B, carry = A · B. A full adder also takes a carry-in:

sum=A⊕B⊕CinCout=AB+Cin(A⊕B)\text{sum} = A \oplus B \oplus C_{in} \qquad C_{out} = AB + C_{in}(A \oplus B)
carrycarrycarryFA bit 3FA bit 2FA bit 1FA bit 0
4-bit ripple-carry adder: each carry waits for the previous one.

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).
sout0a1b
2:1 MUX: out = s̄·a + s·b

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:

FlagMeaningUsed for
Z (zero)Result is 0if (a == b) → subtract and test Z
N (negative)Top bit is 1Signed comparisons
C (carry)Unsigned overflow out of the top bitUnsigned comparisons, multi-word arithmetic
V (overflow)Signed overflowDetecting signed overflow
A (64 bits)B (64 bits)ALUadd · sub · and · or · xor · shiftop coderesultflags Z N C V

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.
Key takeaways
  • 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.
Interview questions
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.