How does a computer decide?
So far, with 0s and 1s, we've stored numbers and letters. But a computer doesn't just store things. It judges: yes or no. Let me show you where that judgment begins.
Two inputs, one output
Every judgment a computer makes
begins in one tiny part.
Its name is the logic gate.
It takes two switches as input,
and by a fixed rule
puts out a single on or off.
Try the three basic gates
with your own hands.
The name gate fits because, like a real gate, it lets a signal through or holds it back depending on the inputs.
Toggle the switches on and off.
What you just touched
is the smallest unit of judgment
that makes up a computer.
Each of these little parts
turns on and off
by a fixed rule.
This rule never changes and never tires, so the same inputs always give the same answer.
And, or, not
The three gates have different personalities.
AND is picky.
It turns on only if both are on.
OR is generous.
Even one being on turns it on.
NOT is contrary.
It flips whatever comes in.
And NOT has just one hand, so unlike the other two it takes only a single input.
AND · and
Like a safety guard that turns on only when you grip both handles at once, it turns on only if both are on.
OR · or
Like getting in whether the front or the back door is open, even one being on turns it on.
NOT · not
A contrarian that goes off when pressed and on when released. It flips the incoming signal.
The personalities of the three gates.
That's all, just these three.
Amazingly,
every judgment every computer makes
is built by mixing these three.
Any judgment, however many conditions it has, is in the end a mix of yes and no, so these three can express all of it.
Put the cases in a table
A gate's rule
sums up neatly in one table.
Since the inputs are two ons and offs,
there are exactly four cases.
For each case, what the output is
is written down.
This is called a truth table.
Each added input doubles the number of cases, so three inputs make eight rows and four make sixteen.
| Input A | Input B | Output |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
The truth table of the AND gate.
One look at the table makes it clear.
AND, on the last row alone,
only when both are on
does the output turn on.
Even judgments that looked complex
come down to a few tables like this.
OR's table is the opposite picture, on in every row except the top one, where both are off.
Chain gates and they get smarter
One gate is simple,
but chain several together
and you can build far more complex judgments.
For example, a judgment that turns on
"only when the two inputs differ."
One gate can't do this,
but a few chained together can.
This judgment of whether two things differ is the very piece used to compare two values and to add them.
A combination that turns on only when they differ.
Stack simple gates like this,
piece by piece,
and you add numbers,
remember values,
and in the end you get a CPU.
Next time, with these very gates,
we'll build a machine that adds.
The wonder is that nothing new is ever needed from here, only how many of the same three gates you use and how you arrange them changes.