Boolean logic and logic gates

A computer, right down at the hardware, is made of billions of tiny switches. Each switch is either on or off — and, just as with binary numbers, we write those two states as 1 (on, or true) and 0 (off, or false). There is nothing in between: a wire is either carrying a "high" voltage or it isn't.

Working with values that can only be 1 or 0 — true or false — is called Boolean logic, after the mathematician George Boole. On its own a single 1 or 0 isn't very useful. The magic happens when we combine them: take two switches and decide what a third wire should do based on them. The little pieces of hardware that do exactly that are called logic gates.

What is a logic gate?

A logic gate is a tiny electronic building block. It has one or more inputs (each a 1 or a 0) and a single output (also a 1 or a 0). The gate follows one fixed rule: for every possible combination of inputs it always produces the same output. Wire thousands of these gates together and you can build adders, memory, and eventually a whole processor.

Almost everything is built from just three core gates: AND, OR and NOT. Each has its own standard drawn symbol, so engineers can sketch a circuit the way you'd sketch a map. Let's meet them one at a time.

The AND gate — needs both

An AND gate outputs 1 only if both of its inputs are 1. If either input is 0 — or both are — the output is 0. Think of two switches wired one after the other in a torch: the bulb lights only when the first and the second switch are on.

The symbol is easy to remember: the flat back and rounded "D" front make it look like a capital D for… well, it just needs both, so it's the fussy one.

The OR gate — needs either

An OR gate outputs 1 if either input is 1 (or if both are). It only outputs 0 when both inputs are 0. Picture two switches wired side by side: pressing this one or that one lights the bulb.

Its symbol has a curved back and a pointed front — a rounded arrowhead. Where AND is fussy, OR is generous: it says "yes" to almost everything.

The NOT gate — flips its input

A NOT gate (also called an inverter) is the simplest of all: it has a single input and simply flips it over. A 1 becomes a 0, and a 0 becomes a 1. That's the whole rule.

The symbol is a triangle pointing in the direction the signal flows, with a small circle (called a "bubble") on the tip. That bubble is the key detail — in circuit diagrams a little bubble on any gate always means "and then invert the output". Miss the bubble and you've just drawn a plain buffer that does nothing at all.

You already know these — from code

If you've done any programming, these three gates will feel familiar, because they are exactly the logical operators you use in an if statement — just built out of hardware instead of software:

Treating true as 1 and false as 0, this program prints out the rule for every gate. Press Run and check each line against the lists above:

// 1 = true (on), 0 = false (off) for (const a of [0, 1]) { for (const b of [0, 1]) { const AND: number = a && b ? 1 : 0; // && → AND gate const OR: number = a || b ? 1 : 0; // || → OR gate console.log("a=" + a + " b=" + b + " | AND=" + AND + " OR=" + OR); } } console.log("NOT 0 = " + (!0 ? 1 : 0)); // ! → NOT gate console.log("NOT 1 = " + (!1 ? 1 : 0));

So when you write if (tallEnough && oldEnough), deep down the processor is feeding those two booleans into a real AND gate and branching on its output.

George Boole was an English mathematician who, in 1854, worked out an entire algebra using only the values "true" and "false" — long before electronics existed. For nearly a century it looked like pure maths with no use. Then in the 1930s a young engineer named Claude Shannon realised Boole's true/false algebra was the perfect way to describe electrical switching circuits. That single insight is the foundation every digital computer is built on.

The classic mistake is muddling up AND and OR:

And be careful with OR itself: in Boolean logic OR is inclusive. In everyday English "tea or coffee?" usually means one or the other, not both — but the logic OR gate is not like that. 1 OR 1 is still 1, not 0. "Either input on — including both — gives an output of on." (The gate that means "one or the other but not both" is a separate gate called XOR, for another day.)

What comes next

We've been listing each gate's behaviour as bullet points, but there's a tidier way to write down exactly what a gate does for every possible input: a truth table. A truth table has one row for each combination of inputs and a column for the output, so the whole rule of a gate fits in a tiny grid. That's the perfect next step — truth tables — and it's also how you work out what a circuit made of several gates joined together will do.