Find a course

Module 2: Logic Gates and CircuitsLesson 4 of 1113 min

Transistors and Logic Gates

See how a transistor works as an electrical switch, then learn the AND, OR, NOT and XOR gates and read their truth tables like an engineer.

Step 1 of 13Learn

Switches that flip switches

A light switch on the wall is controlled by your finger. A transistor is a switch controlled by electricity: a small voltage on one terminal (the gate) decides whether current can flow between the other two. No moving parts, no finger, and it can flip billions of times per second.

That one property changes everything. Because the output of one transistor can control the input of another, switches can control switches. Arrange a few of them cleverly and you get circuits that make decisions, such as let current through only if both inputs are on.

Transistors have shrunk astonishingly. The first ones in 1947 were the size of a thumbnail. Today's processor chips pack tens of billions of transistors onto a piece of silicon about the size of a fingernail, with features measured in nanometers, far smaller than a virus. For decades the number of transistors on a chip roughly doubled every two years, a trend known as Moore's law. That pace has slowed, but it explains why a phone today outperforms room-sized supercomputers from a few decades ago.

Step 2 of 13Learn

The three basic gates

A logic gate is a small circuit, usually a handful of transistors, that takes one or more bits as input and produces one bit as output. We describe gates with truth tables that list every input combination.

NOT flips its single input: 0 becomes 1, 1 becomes 0.

AND outputs 1 only if all inputs are 1.

OR outputs 1 if at least one input is 1.

ABA AND BA OR B
0000
0101
1001
1111

These match everyday reasoning. A car starts if the key is present AND the brake is pressed. An alarm sounds if the front door OR the back door opens. A seatbelt warning light glows when the belt is NOT fastened.

A careful point about OR: in logic it is inclusive. A OR B is true when both are true. In everyday speech, “soup or salad” usually means one or the other, not both. That exclusive meaning has its own gate, XOR.

Step 3 of 13Compare

Truth tables side by side

AspectA AND BA OR B
A=0, B=000
A=0, B=101
A=1, B=001
A=1, B=111

Step 4 of 13Practice · Match the pairs

Match each gate to the situation it describes.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. AND
  2. OR
  3. NOT
  4. XOR

Step 5 of 13Note

Step 6 of 13Learn

XOR, NAND and building anything

XOR (exclusive OR) outputs 1 when its inputs are different: 0 XOR 1 = 1, 1 XOR 0 = 1, but 1 XOR 1 = 0 and 0 XOR 0 = 0. XOR shows up everywhere: in adding binary numbers, in error checking, and in encryption.

NAND means NOT AND: it outputs 0 only when all inputs are 1. NOR means NOT OR. These two have a remarkable property: each is universal. Using only NAND gates, you can build NOT, AND, OR, XOR and therefore any digital circuit at all. For example, connect both inputs of a NAND to the same signal and you get a NOT gate.

That's why chip designers love NAND: it is small and fast in the CMOS transistor technology used in nearly all modern chips, and it is enough to build everything else.

Gates combine into Boolean expressions, named after the mathematician George Boole. The expression (A AND B) OR C is true if C is true, or if A and B are both true. You can evaluate any such expression by writing out a truth table with one row per input combination: 2 inputs need 4 rows, 3 inputs need 8 rows, n inputs need 2n rows.

Step 7 of 13Practice · True or false

1 XOR 1 equals 1, because XOR is a kind of OR and both inputs are on.

Step 8 of 13Note

Step 9 of 13Diagram

NOT(A AND B) OR C, with A=1, B=1, C=0

NOT(A AND B) OR C, with A=1, B=1, C=0Process diagram: A AND B = 1 → NOT 1 = 0 → 0 OR C = 0 OR 0 → Result: 0.A AND B = 1NOT 1 = 00 OR C = 0 OR 0Result: 0

Tap a shape with to see more.

A AND B = 1
NOT 1 = 0
0 OR C = 0 OR 0
Result: 0

Innermost brackets first, then outward, just like arithmetic.

Step 10 of 13Practice · Work it out

Evaluate (A OR B) AND (NOT C) when A = 0, B = 1 and C = 1. Answer 0 or 1.

Step 11 of 13Learn

Why logic is enough to compute

It may seem impossible that AND, OR and NOT could run a video game. The trick is that any rule that maps input bits to output bits can be written as a combination of these gates.

Suppose you want a circuit with three inputs that outputs 1 when at least two inputs are 1, a majority vote. Writing the cases: (A AND B) OR (A AND C) OR (B AND C). Three AND gates and two OR gates do the job. Engineers use exactly this kind of reasoning, helped by software, to design circuits with billions of gates.

Numbers are bits, letters are bits, colors are bits. If every task can be expressed as transform these input bits into those output bits, then gates can do it. Adding two numbers, comparing them, choosing which instruction to run next: each of these is a fixed rule about bits.

There is one missing ingredient. Gates on their own forget everything the moment their inputs change. A real computer must also remember values from one moment to the next. In the next lesson you'll see how a small loop of gates can store a bit, and how gates add binary numbers.

Step 12 of 13Practice · Diagram

A majority-vote circuit

Output is 1 when at least two of A, B, C are 1.

If A=1, B=0, C=1, which AND gate turns the output on?

Tap the shape that answers the question, then press Check.

A majority-vote circuitHierarchy diagram: OR gates: output, A AND B, A AND C, B AND C.OR gates: outputA AND BA AND CB AND C
OR gates: output
A AND B
A AND C
B AND C

Step 13 of 13Practice · Choose one

A door control has three inputs: A (badge valid), B (correct PIN), C (manual override by security). The door should open if the badge AND PIN are both correct, or if security uses the override. Which expression fits?