Electronics / Logic gates (AND, OR, NOT, XOR)

Logic gates (AND, OR, NOT, XOR)

Four small circuits that turn 1s and 0s into decisions: what NOT, AND, OR and XOR each do, how transistors make them, and how NAND alone can build them all.

Beginner8 steps~7 min
1 — a wire at high voltage
0 — a wire at low voltage
Gate — turns input bits into one output bit
// THE SHORT VERSION

In 30 seconds

A logic gate is a tiny circuit that takes one or two bits and outputs one bit by a fixed rule. NOT flips its input. AND gives 1 only if both inputs are 1. OR gives 1 if at least one is. XOR gives 1 if exactly one is.

A truth table lists every input combination and its output, so it describes a gate completely.

Each gate is a handful of transistors acting as switches. Wire gates together, output to input, and you can build anything a computer does; NAND gates alone are enough.

Logic gates (AND, OR, NOT, XOR), step by step

STEPS01 / 08

The transistor circuit shown is the CMOS design used in almost all modern chips, drawn as simple switches. Gates with three or more inputs exist too; they follow the same rules.

STEP 01 / 08 NOT flips a bit
THE MATH
OUT = NOT A 1 -> 0 0 -> 1
  1. Step 1: NOT flips a bit. A logic gate is a small circuit whose output depends only on its inputs. Wires carry one of two voltages, read as 1 (high) and 0 (low). The simplest gate, NOT, has one input and outputs the opposite. Its symbol is a triangle with a small circle; the circle means “inverted”.
  2. Step 2: AND needs both. AND has two inputs and outputs 1 only when both are 1. Two inputs can be set four ways, so its truth table has four rows, and only the last gives 1. The symbol has a flat back and a round front.
  3. Step 3: OR needs at least one. OR outputs 1 when at least one input is 1, including when both are. Only the row with two 0s gives 0. Its symbol has a curved back and a pointed front.
  4. Step 4: XOR: one or the other. XOR, “exclusive or”, outputs 1 when exactly one input is 1. It differs from OR only in the last row: two 1s give 0. The symbol is an OR gate with a second curve behind it. XOR is also the sum bit you get when adding two bits, which is why adders are built around it.
  5. Step 5: NAND: AND, then NOT. Put the inverting circle on an AND gate and you get NAND, “not AND”. Its output column is the AND column upside down: 0 only when both inputs are 1. It looks like a minor variation, but it is the gate chips are most naturally built from.
  6. Step 6: Inside: four transistors. A gate is made of transistors, which are switches controlled by a voltage. A NAND uses four. The two lower ones conduct when their input is 1 and sit in a chain to ground; the two upper ones conduct when their input is 0 and sit side by side to the supply. Only two 1s complete the chain and pull the output to 0.
  7. Step 7: NAND builds the others. NAND is a universal gate: any other gate, and so any logic circuit, can be wired from NANDs alone. Join both inputs and it acts as NOT. Follow a NAND with that NOT and you get AND. Invert both inputs first and the NAND behaves as OR. NOR has the same property.
  8. Step 8: Combine gates. Feeding the output of one gate into the input of another builds new behaviour. Here an OR and a NAND both read A and B, and an AND combines their answers. Check all four rows and the result matches XOR exactly. Processors are the same idea repeated: billions of gates wired output to input.
VALUES SO FAR
NOTflips the bitgate
AND····gate
OR····gate
XOR····gate
NAND····gate
NAND····built
NAND····rule
XOR····built

Spotted a mistake on this page?