How does a computer add numbers
Wire together a few of the AND, OR, and NOT gates from the last lesson, and, amazingly, you get a machine that adds. This is exactly where a computer's arithmetic begins.
In the world of 0s and 1s, 1+1 is 10
Before building addition into a circuit, let's see how adding works with 0s and 1s.
0+0 is 0, and 0+1 is 1. So far, so easy.
But what about 1+1? Binary has no digit 2, so you carry to the next place and get 10.
That's not decimal ten; it's binary "one-zero."
See the result of adding two bits, case by case.
So adding two bits gives two results.
One is the value that stays in that place, and the other is the value passed on to the next place.
These two are called the sum and the carry.
The sum is XOR, the carry is AND
Here's the amazing part: that rule can be built directly from the gates in the last lesson.
The sum is 1 only when the two bits differ, which is exactly XOR ("different").
The carry is 1 only when both are 1, which is exactly an AND gate.
Two gates and you're done.
Half adder. Switch the inputs on and the sum and carry change right away.
This little machine that adds two bits and puts out a sum and a carry is called a half adder.
With just two gates, you've built the simplest adder in the world.
Real addition includes the carry-in
But the half adder has a gap.
When you add numbers with several places, you also have to add the carry coming up from the place below.
So there are three inputs: A, B, and the carry from below.
The machine that adds all three is called a full adder.
Full adder: three inputs.
A full adder is made from two half adders plus one OR gate.
You build slightly bigger parts from small ones, then wire those together into even bigger ones.
That's how a computer is built from start to finish.
Chain them and you can add big numbers
One full adder adds just one place.
To add four-place or eight-place numbers, line full adders up side by side.
The carry from each place is passed straight into the next place as an input.
Below, you can switch on two numbers and add them yourself.
Switch on two numbers and add them (4 bits).
Switch on the top and middle rows, and the sum appears by itself in the bottom row.
No person does the math; the gates build the answer by passing carries along.
Addition was gates all along
A computer doing arithmetic was never magic.
It's just AND, OR, and NOT gates wired together in a fixed pattern.
Flip the switches, current runs through the gates, and at the end the answer lights up.
From small parts to big ones.
Big functions grow out of small parts like this.
In the next lesson, we'll meet a circuit that holds on to a value even while current keeps flowing: a circuit that remembers.