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
The half adder is the conceptual starting point: it shows how a single column adds with just two gates. But it has no way to accept a carry coming in from a lower column, so on its own it can only ever add the very first bit. To chain columns together — and to support subtraction, where even the first column needs a carry-in of 1 — every position uses a full adder that accepts a carry-in as a third input. That's why the simulator below shows a full adder (FA0) even at bit 0.
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 — these compute every column's carry directly from the inputs in one shot, instead of waiting for it to ripple through one bit at a time.
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.
One twist: during subtraction the Carry Out flag means the opposite of what you might expect. It acts as an inverted borrow — carry-out = 1 means no borrow was needed (A ≥ B), while carry-out = 0 means a borrow occurred (A < B, so the true answer is negative).
Two Flags: Carry vs. Overflow
The simulator lights two status flags after each operation, and they answer different questions:
- Carry Out is about unsigned numbers. It lights when an addition's result is too big to fit in the chosen bit width — the value wrapped around past the top (e.g. 255 + 1 in 8-bit). It is simply the carry that falls off the most significant bit.
- Overflow is about signed (two's complement) numbers. It lights when the result's sign comes out wrong — for example 127 + 1 in 8-bit landing on −128. Technically it fires when the carry into the sign bit differs from the carry out of it.
In Simple Terms: Which Flag Should I Watch?
If you're treating the bits as plain positive numbers, watch Carry Out. If you're treating them as signed numbers that can go negative, watch Overflow. The hardware always computes both — it's up to the program to know which interpretation it meant.