Adding like in primary school
When you add 47 + 38 on paper you add the rightmost column, write the digit, carry the extra and move left. A binary adder does exactly this, one full adder per column.
The full adder
A full adder takes three bits, A, B and carry-in, and produces two:
sum = A XOR B XOR carry-in
carry-out = (A AND B) OR (carry-in AND (A XOR B))
| A | B | Cin | Sum | Cout |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
Chaining them
Connect the carry-out of each adder to the carry-in of the next. The first carry-in is 0. The carry ripples upward, which gives the circuit its name.
Example: 11 + 6 = 1011 + 0110
| Bit | A | B | Carry in | Sum | Carry out |
|---|---|---|---|---|---|
| 0 | 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 0 | 0 | 1 |
| 2 | 0 | 1 | 1 | 0 | 1 |
| 3 | 1 | 0 | 1 | 0 | 1 |
The final carry becomes bit 4, giving 10001 = 17.
Speed
Bit 3 cannot produce its sum until bit 2’s carry is ready, which waits for bit 1, and so on. The delay is O(n). Real CPUs use carry-lookahead adders that work out all carries at once from “generate” (A AND B) and “propagate” (A XOR B) signals, so 64-bit addition still takes only a few gate delays.
Overflow
With unsigned numbers, a carry out of the top bit means the result does not fit. With two’s complement numbers, overflow happens when two numbers of the same sign give a result of the opposite sign.
Code
def ripple_add(a, b, n=4):
carry, total = 0, 0
for i in range(n):
x, y = (a >> i) & 1, (b >> i) & 1
total |= (x ^ y ^ carry) << i
carry = (x & y) | (carry & (x ^ y))
return total | (carry << n)
print(ripple_add(11, 6)) # 17
Common mistakes
- Forgetting the carry-in when filling the truth table: a full adder has three inputs, not two.
- Reading the sum from left to right. Bit 0 is the rightmost bit.
- Dropping the final carry. In an unsigned add it is the fifth bit of the answer.
Complexity at a glance
| Case / operation | Time | Why |
|---|---|---|
| Add two n-bit numbers | O(n) | Each carry waits for the previous adder, so the delay is n full-adder delays. |
| Carry-lookahead adder | O(log n) | Computes all carries in parallel using generate and propagate signals. |
| Extra space | n full adders (about 5 gates each) |
Quick check
Test yourself — pick an answer to see if you got it.
1. What are the inputs of a full adder?
A full adder adds three bits (A, B and the carry from the previous position).
2. What is the sum bit of a full adder with inputs 1, 1 and carry-in 1?
1 + 1 + 1 = 3 = binary 11, so the sum bit is 1 and the carry-out is 1.
3. Why is a ripple-carry adder slow for wide numbers?
Bit i cannot finish until the carry from bit i − 1 arrives, so the delay is proportional to the number of bits.
4. A 4-bit unsigned adder produces a carry-out of 1 from the top bit. What does it mean?
The true sum is at least 16, which needs a fifth bit.