Digital Systems 3 min read

Logic gates

A logic gate is a tiny circuit that turns one or two input bits into one output bit. A handful of gates (AND, OR, NOT, XOR, and their inverted forms NAND and NOR) are the building blocks of every digital circuit, and the C bit operators are named after them.

Six gate symbols in two rows. AND outputs 1 only when both inputs are 1. OR outputs 1 when at least one input is 1. NOT outputs the opposite of its single input. XOR outputs 1 when the two inputs differ. NAND is an AND followed by NOT, and NOR is an OR followed by NOT; a small circle on the output marks the inversion.
Six gates, each shown by its symbol and the rule for when its output is 1.

Six gates to know

Each gate has a symbol and a rule. A small circle on an output means the result is inverted: NAND is AND followed by NOT, and NOR is OR followed by NOT.

GateOutput is 1 whenIn C
ANDboth inputs are 1a & b
ORat least one input is 1a | b
NOTthe input is 0~a
XORthe inputs differa ^ b
NANDnot both inputs are 1~(a & b)
NORboth inputs are 0~(a | b)

The same gates in your registers

When you write to a register, you are using gates, one per bit, in parallel. AND with a mask keeps some bits and clears the rest, OR sets bits, and XOR toggles them:

reg |=  (1u << 3);    // OR: set bit 3
reg &= ~(1u << 3);    // AND with NOT: clear bit 3
reg ^=  (1u << 3);    // XOR: toggle bit 3

XOR is also an equality check: it outputs 1 exactly when its two inputs differ, so a ^ b is zero only if all bits match.

NAND builds everything

NAND is a universal gate: any other gate can be made from NANDs alone. Tie both inputs together and it becomes NOT, since 1 NAND 1 = 0 and 0 NAND 0 = 1. Follow a NAND with a NOT and you have AND. In CMOS chips NAND and NOR are also the simplest cells to build, which is why real designs lean on them.

Gates are not instant. Each one adds a small propagation delay, and the datasheet of the chip gives the real figure. Long chains of gates are what limit how fast a circuit can run.