Logic gates - teaching switches to make decisions
How a handful of transistors combine into AND, OR and NOT gates, and how Boolean algebra lets you calculate with true/false the same way you calculate with numbers.
From "on/off" to "yes/no"
A single switch just stores a bit, on or off. But if you want a computer to make even the simplest decision — "turn the light on only if BOTH the switch is flipped AND it's dark outside" — you need to combine switches so their outputs depend on their inputs in a specific, useful way. These combinations are called logic gates, and they're built from just a few transistors each.
We label the two states1 (true/on) and 0 (false/off), and describe every gate with a truth table: a complete list of every possible input combination and the output it produces.
The three basic gates
NOT gate — the simplest possible gate. It has one input and flips it: on becomes off, off becomes on. It's drawn as a triangle with a small circle ("bubble") at the tip.
| A | NOT A |
|---|---|
| 0 | 1 |
| 1 | 0 |
AND gate — output is 1 only when every input is 1. Think of two light switches wired in series, on the same circuit, one after the other — the light only turns on if both are flipped on.
| A | B | A AND B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
OR gate — output is 1 if at least one input is 1. Think of two switches wired in parallel — flipping either one lights the bulb.
| A | B | A OR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Every other gate you'll ever meet — NAND, NOR, XOR, XNOR — is built by combining these three, or is a small variation worth knowing on its own.
NAND and NOR: "AND/OR, then flip the answer"
NAND (NOT-AND) is an AND gate immediately followed by a NOT — its output is the exact opposite of AND's, at every single row:
| A | B | A AND B | A NAND B |
|---|---|---|---|
| 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 0 |
NOR (NOT-OR) is likewise OR followed by a NOT:
| A | B | A OR B | A NOR B |
|---|---|---|---|
| 0 | 0 | 0 | 1 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 0 |
These two might look like an afterthought, but they're actually the most important gates in real chip manufacturing: NAND and NOR are each individually universal, meaning either one, wired together in different combinations with itself, can build a NOT, an AND, an OR, or literally any other logic function that exists. This is precisely why the SR latch in the next lesson is built from cross-wired NOR (or NAND) gates instead of separate AND/OR/NOT gates — factories only need to mass-produce one single, simple gate design.
XOR: "exactly one, not both"
XOR (exclusive OR) outputs 1 only when its inputs differ. It shows up constantly in arithmetic circuits, because it behaves exactly like single-bit addition without carrying.
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Notice XOR matches binary addition ignoring carry: , , , (write 0). That last row is exactly why XOR is the core building block of an adder circuit, which we'll build in the next lesson.
Boolean algebra: arithmetic for true/false
Just like ordinary algebra lets you simplify into without redrawing the whole problem, Boolean algebra lets you simplify logic expressions symbolically. We write AND as (or nothing, like multiplication), OR as , and NOT as an overline or prime.
A few laws that mirror ordinary algebra:
And a few laws that have no ordinary-algebra equivalent, because Boolean variables only ever hold 0 or 1:
The most powerful law is De Morgan's Theorem, which lets you push a NOT through an AND or OR by flipping the operator:
Don't just take this on faith — a truth table proves it directly, by computing both sides of the first equation for every possible A and B and checking they always agree:
| A | B | (NAND) | Match? | |
|---|---|---|---|---|
| 0 | 0 | 1 | 1 | ✓ |
| 0 | 1 | 1 | 1 | ✓ |
| 1 | 0 | 1 | 1 | ✓ |
| 1 | 1 | 0 | 0 | ✓ |
Every row matches, so the two expressions really are the exact same function, just written two different ways. This isn't just a party trick — it's why a circuit built entirely from NAND gates (AND followed by NOT) can implement any logic function: De Morgan's Theorem is precisely the tool that lets you rewrite an OR (and, by extension, a NOT) purely in terms of NAND, which matters enormously for manufacturing, since factories can mass-produce one single gate type instead of several.
Worked example: simplifying a real expression
Suppose a lock should open () when: the key is correct () AND (either the manager is present () OR it's during business hours ()). As a Boolean expression:
Suppose we're handed the messier, unsimplified circuit from an engineer who built it directly from the requirement "opens if (key AND manager) OR (key AND hours)." Using the distributive law (yes, it works in Boolean algebra too):
Both expressions produce an identical truth table, but the second version needs one AND gate for folded in, versus two separate AND gates plus one OR gate in the first — fewer gates, less silicon, less power, same behavior. This kind of simplification, done automatically by software at massive scale, is a large part of what modern chip design tools spend their time doing.
Common mistakes
- Confusing OR with XOR. "1 or 2 sugars" in English usually means exactly one, but logical OR includes the case where both inputs are 1. If you mean "exactly one," you need XOR.
- Forgetting De Morgan flips the operator. is , not — a very common sign-flip mistake.
- Treating Boolean as ordinary addition. in Boolean OR, not 2 — the symbol is borrowed but the arithmetic is different.
Summary
Logic gates are small groups of transistors that implement basic decisions: NOT flips a bit, AND requires every input to be true, OR requires at least one, and XOR requires exactly one. Boolean algebra lets you manipulate these decisions symbolically using laws similar to ordinary algebra, plus a few extra ones like De Morgan's Theorem that only make sense for two-valued variables. Simplifying a Boolean expression before building it in hardware directly reduces the number of gates needed — the seed of everything from a single adder to an entire CPU.