When your code runs total = 77 + 45, a circuit inside the CPU adds two rows of bits and gives back a third. There's no little calculator in there, only logic gates. The circuit that does it is built from a small repeating unit called a full adder, which itself is built from two half adders. This lesson shows how the half adder and full adder work, gate by gate, and then chains eight of them into a real 8-bit adder that you'll run in Python.
This is the point where logic gates stop being truth tables and start doing arithmetic, and it's a favourite topic in exams and hardware interviews.
What you need first: logic gates and truth tables and bits, bytes and binary.
Key terms
- Sum bit: the result bit for one column of an addition.
- Carry: the bit passed to the next column when a column's total is 2 or more.
- Half adder: a circuit that adds two bits and gives a sum and a carry.
- Full adder: a circuit that adds three bits (two inputs plus a carry in) and gives a sum and a carry out.
- Ripple-carry adder: several full adders in a chain, each passing its carry to the next.
- Overflow: when the result needs more bits than the adder has.
Binary addition by hand
Binary addition works like school addition, column by column from the right, except each column can only hold 0 or 1:
| Column adds | Total | Write down | Carry |
|---|---|---|---|
| 0 + 0 | 0 | 0 | 0 |
| 0 + 1 | 1 | 1 | 0 |
| 1 + 0 | 1 | 1 | 0 |
| 1 + 1 | 2 | 0 | 1 |
| 1 + 1 + 1 (with a carry in) | 3 | 1 | 1 |
77 + 45 in 8 bits:
carry: 0 0 0 1 1 0 1
0 1 0 0 1 1 0 1 (77)
+ 0 0 1 0 1 1 0 1 (45)
-----------------
0 1 1 1 1 0 1 0 (122)
The carry row shows what each column receives from the column on its right. The rightmost column has nothing coming in: 1 + 1 = 2, so write 0 and carry 1.
Each column does the same small job: add two bits plus whatever was carried in, write down one bit, and carry one bit to the left. A circuit that does that job once can be copied for every column.
print(format(77, "08b"), format(45, "08b"), format(77 + 45, "08b"))
01001101 00101101 01111010
The half adder
Start with the simplest case: add two bits, A and B, with no carry coming in. Compare the outputs with the gates you know:
| A | B | Sum | Carry |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
- The Sum column is 1 when the inputs differ: that's XOR.
- The Carry column is 1 only when both are 1: that's AND.
Sum = A ⊕ B
Carry = A · B
So a half adder is one XOR gate and one AND gate, side by side, both fed A and B.
def half_adder(a, b):
return a ^ b, a & b # (sum, carry)
for a in (0, 1):
for b in (0, 1):
print(a, b, "->", half_adder(a, b))
0 0 -> (0, 0)
0 1 -> (1, 0)
1 0 -> (1, 0)
1 1 -> (0, 1)
It's called "half" because it can only do the rightmost column. Every other column also has to add a carry from the column on its right, which needs three inputs.
The full adder
A full adder adds three bits: A, B and the carry in (Cin). It outputs a Sum and a carry out (Cout).
| 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 |
- Sum is 1 when an odd number of inputs are 1:
A ⊕ B ⊕ Cin. - Cout is 1 when at least two inputs are 1. That's the majority function from the K-maps lesson:
AB + BCin + ACin. It can also be writtenAB + Cin(A ⊕ B), which reuses a gate.
A full adder from two half adders
The neat way to build it:
- The first half adder adds A and B, giving a partial sum
S1 = A ⊕ Band a carryC1 = AB. - The second half adder adds S1 and Cin, giving the final
Sum = S1 ⊕ Cinand a carryC2 = S1 · Cin. - An OR gate combines the two carries:
Cout = C1 + C2. (They can never both be 1, so OR is enough.)
A ──┐ S1 ──┐
├─[half adder]─┤ ├─[half adder]── Sum
B ──┘ C1 Cin ─┘ C2
│ │
└─────[ OR ]─────┘── Cout
That's 2 XOR, 2 AND and 1 OR gate. In Python, building it from half_adder, and checking every row against ordinary addition:
def half_adder(a, b):
return a ^ b, a & b
def full_adder(a, b, carry_in):
s1, c1 = half_adder(a, b)
total, c2 = half_adder(s1, carry_in)
return total, c1 | c2 # (sum, carry_out)
for a in (0, 1):
for b in (0, 1):
for c in (0, 1):
s, cout = full_adder(a, b, c)
assert 2 * cout + s == a + b + c
print(a, b, c, "->", s, 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
The assert checks the meaning: carry is worth 2, sum is worth 1, and together they must equal the number of 1s that went in.
Chaining full adders: the ripple-carry adder
To add two 8-bit numbers, line up 8 full adders, one per column. Each one's carry out becomes the next one's carry in, so the carry "ripples" from right to left:
A7 B7 A2 B2 A1 B1 A0 B0
| | | | | | | |
[FA7] <-...-[FA2] <----[FA1] <----[FA0] <--- carry in = 0
| \ | | |
S7 carry S2 S1 S0
out
In Python, with each number as a list of bits:
def half_adder(a, b):
return a ^ b, a & b
def full_adder(a, b, carry_in):
s1, c1 = half_adder(a, b)
total, c2 = half_adder(s1, carry_in)
return total, c1 | c2
def to_bits(n, width=8):
return [(n >> i) & 1 for i in range(width)] # least significant bit first
def from_bits(bits):
return sum(bit << i for i, bit in enumerate(bits))
def ripple_add(x, y, width=8):
carry = 0
result = []
for a, b in zip(to_bits(x, width), to_bits(y, width)):
s, carry = full_adder(a, b, carry)
result.append(s)
return from_bits(result), carry # (answer, final carry out)
print(ripple_add(77, 45))
print(ripple_add(200, 55))
print(ripple_add(200, 100))
(122, 0)
(255, 0)
(44, 1)
The first two are correct. The third shows overflow: 200 + 100 = 300, but 8 bits can only hold 0 to 255. The adder keeps the lowest 8 bits (300 − 256 = 44) and sets the final carry out to 1. CPUs record that carry in a flags register, so software can tell the answer didn't fit. This is the same wrap-around you'd see with an 8-bit counter, or an old car's odometer rolling from 99999 to 00000.
Python's own int never overflows like this, because Python joins several machine words together for big numbers. Languages like C and Java do overflow, which causes real bugs.
Making it faster
A ripple-carry adder is simple but slow: the leftmost full adder can't finish until the carry has rippled through every adder to its right. For 64-bit numbers, that's 64 steps in a row. Real CPUs use a carry-lookahead adder, which uses extra gates to work out all the carries at once from the inputs, at the cost of more transistors. Designers make this trade (more gates for more speed) all the time.
Subtraction with the same adder
CPUs don't need a separate subtractor. To work out A − B, they add A to the two's complement of B: flip every bit of B (NOT gates) and add 1 (by setting the first carry in to 1). With 8 bits, 77 − 45:
def half_adder(a, b):
return a ^ b, a & b
def full_adder(a, b, carry_in):
s1, c1 = half_adder(a, b)
total, c2 = half_adder(s1, carry_in)
return total, c1 | c2
def subtract(x, y, width=8):
carry = 1 # the "+1" of two's complement
result = 0
for i in range(width):
a = (x >> i) & 1
b = 1 - ((y >> i) & 1) # NOT gate flips each bit of y
s, carry = full_adder(a, b, carry)
result |= s << i
return result
print(subtract(77, 45))
32
One adder plus a row of NOT gates (switched on only for subtraction) does both jobs. That's how the ALU (arithmetic logic unit) in a CPU handles addition and subtraction; from logic gates to a CPU puts it together.
Practice
- Add 0110 and 0011 by hand in binary. Check in Python.
Show the answer
6 + 3 = 9, which is 1001.
print(format(0b0110 + 0b0011, "04b"))
1001
- Which two gates make a half adder, and which output does each produce?
Show the answer
An XOR gate produces the sum, and an AND gate produces the carry.
- A full adder gets A = 1, B = 0, Cin = 1. What are Sum and Cout? Explain using the count of 1s.
Show the answer
Two inputs are 1, so the total is 2, which is binary 10: Sum = 0, Cout = 1.
- How many full adders does a 32-bit ripple-carry adder need, and why is it slow?
Show the answer
32, one per bit. It's slow because each adder must wait for the carry from the one before, so the last adder waits for 31 others in turn.
- Using
ripple_addwith 4 bits, what does 9 + 9 give? Why?
Show the answer
def half_adder(a, b):
return a ^ b, a & b
def full_adder(a, b, carry_in):
s1, c1 = half_adder(a, b)
total, c2 = half_adder(s1, carry_in)
return total, c1 | c2
def ripple_add(x, y, width):
carry, result = 0, 0
for i in range(width):
s, carry = full_adder((x >> i) & 1, (y >> i) & 1, carry)
result |= s << i
return result, carry
print(ripple_add(9, 9, 4))
(2, 1)
9 + 9 = 18, but 4 bits only hold 0 to 15. The adder keeps 18 − 16 = 2 and sets the carry out to 1: overflow.
Interview questions
What is the difference between a half adder and a full adder?
A half adder adds two bits and produces a sum (XOR) and a carry (AND); it has no carry input. A full adder adds two bits plus a carry in, producing a sum (A ⊕ B ⊕ Cin) and a carry out (AB + Cin(A ⊕ B)), so full adders can be chained for multi-bit addition.
How do you build a full adder from half adders?
Use two half adders and an OR gate: the first adds A and B; the second adds that partial sum and the carry in, giving the final sum; the OR gate combines the two half adders' carries into the carry out.
Why is a ripple-carry adder slow, and what is the alternative?
Each stage must wait for the carry from the previous stage, so delay grows linearly with the number of bits. A carry-lookahead adder computes carries in parallel from generate (AB) and propagate (A ⊕ B) signals, trading extra gates for much lower delay.
Key takeaways
- Binary addition works column by column: each column adds bits plus a carry, writes one bit, and carries one bit.
- A half adder adds two bits: Sum = A XOR B, Carry = A AND B.
- A full adder adds three bits (with carry in): Sum = A ⊕ B ⊕ Cin, Cout = AB + Cin(A ⊕ B); it's two half adders and an OR.
- Chaining full adders gives a ripple-carry adder; a final carry out of 1 means overflow.
- Subtraction uses the same adder with B's bits flipped and a carry in of 1 (two's complement).
Frequently asked questions
Why is it called a half adder?
Because it does only half the job of one column of addition: it adds two bits but can't take a carry from the previous column. Two half adders (plus an OR gate) make one full adder.
Do CPUs really add with gates like this?
Yes. The adder inside every CPU's ALU is built from logic gates following these same equations, usually with carry-lookahead or similar designs for speed. They work on 64 bits at a time and finish in a fraction of a nanosecond.
How does a computer multiply?
By repeated shifting and adding, like long multiplication in binary: for each 1 bit in one number, add a shifted copy of the other. Modern CPUs have dedicated multiplier circuits made of many adders working in parallel.
What to learn next
- Learn how circuits choose between inputs in multiplexers and decoders.
- See the adder inside a CPU in from logic gates to a CPU.
- Revise the carry logic in Boolean algebra and K-maps.