How Computers Add Numbers
Every time your computer adds two numbers, it uses an Arithmetic Logic Unit (ALU) — a circuit built from simple logic gates. Each gate performs a tiny boolean operation (AND, OR, XOR), and by combining them, the ALU can add, subtract, and perform bitwise operations on binary numbers.
In Simple Terms
Binary addition works just like decimal addition, but with only two digits: 0 and 1. When you add 1 + 1, you get 10 in binary (that's 2 in decimal) — the "1" carries over to the next column, exactly like carrying in regular addition. A full adder is a small circuit that adds two bits plus a carry-in, producing a sum bit and a carry-out. Chain several together and you get a ripple carry adder that can add multi-bit numbers.
Logic Gates Used
| A | B | Out |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
| A | B | Out |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
| A | B | Out |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
In Simple Terms: Why XOR for Addition?
If both inputs are 0, the sum is 0. If one is 1, the sum is 1. If both are 1, the sum is 0 with a carry. The sum bit is 1 only when the inputs differ — and that's exactly what XOR does. It's the natural gate for single-bit addition.
The Half Adder: A Simpler Building Block
Before understanding a full adder, consider the half adder — the simplest addition circuit. It adds two single bits with no carry-in:
- Sum = A XOR B
- Carry = A AND B
A half adder handles the first bit position (where there's no incoming carry). For every subsequent bit, we need a full adder that also accepts a carry-in from the previous position.
Inside a Full Adder
Each full adder uses 2 XOR gates, 2 AND gates, and 1 OR gate:
- Sum = A XOR B XOR Carry-in
- Carry-out = (A AND B) OR (Carry-in AND (A XOR B))
The carry-out of each full adder feeds into the carry-in of the next, creating a "ripple" effect — hence the name ripple carry adder. Use the simulator below to watch this propagation in action.
Full Adder Truth Table
| A | B | Cin | Sum | Cout |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
In Simple Terms: The Ripple
Imagine adding 999 + 1 by hand. The carry ripples left through every 9, turning each into a 0 before finally settling. A ripple carry adder works the same way — each bit must wait for the carry from the previous one before it can produce its final sum. Simple and elegant, but slow for large numbers, which is why real CPUs use fancier carry logic like carry-lookahead adders.
How Subtraction Works: Two's Complement
How does an adder subtract? The trick is two's complement: instead of building a separate subtraction circuit, we reuse the same adder with a simple transformation.
To compute A − B:
- Invert every bit of B (flip 0s to 1s, and 1s to 0s). This gives the one's complement.
- Add 1 by setting the carry-in of the first adder to 1.
- Add normally: A + (inverted B) + 1 = A − B.
For example, in 4-bit: 7 − 3 → 0111 + 1100 + 1 = 0100 (4). Select SUB in the simulator to see this in action — notice the inverted B inputs and the carry-in set to 1.