Every arithmetic circuit in a processor grows out of one small cell that adds three bits. This lesson pairs with Exploring How Computers Work by Sebastian Lague and Learn how computers add numbers and build a 4 bit adder circuit by Ben Eater, linked under Watch & read. Both cover binary addition and an adder wired from gates. You build the half adder, the full adder, a ripple-carry adder with an overflow flag, negation, subtraction and a carry-lookahead adder, and check them against Python integers.
Words are lists of bits with the least significant bit first, so to_bits(6, 4) is [0, 1, 1, 0]. That order is the order in which carries travel, which makes the loops short. The gates of the earlier lessons are written here as the Python operators ^, & and |.
A half adder adds two bits. In binary 1 + 1 = 10, so the sum bit is XOR and the carry is AND. It has no input for a carry from below, so it cannot be chained. A full adder fixes that with two half adders: the first adds a and b, the second adds the carry in to that sum, and an OR merges the two carries. The OR is safe because both half adders can never carry at once.
Chain one full adder per bit and the carry out of each stage becomes the carry in of the next. That is a ripple-carry adder.
The worked example is 5 + 3 in four bits, which is 0101 + 0011. The snippet runs the same stages as the figure.
def full_adder(a, b, c):
p = a ^ b
return p ^ c, (a & b) | (p & c)
a, b = [1, 0, 1, 0], [1, 1, 0, 0] # 5 and 3, least significant bit first
carry = 0
for i, (x, y) in enumerate(zip(a, b)):
s, carry_out = full_adder(x, y, carry)
print(f"bit {i}: {x} + {y} + {carry} -> sum {s}, carry {carry_out}")
carry = carry_outThe adder does not know whether the bits are unsigned or signed, and it gives two flags. The carry out of the top bit means an unsigned result did not fit. For a signed result, compare the carry into the top bit with the carry out of it: if they differ, the result did not fit. In the example no carry leaves bit 3 but one enters it, so the overflow flag is 1: 8 is outside the signed range -8 to 7. For 15 + 1 a carry both enters and leaves the top bit, so the sum 0000 is the correct signed result -1 + 1 even though the carry out is 1.
Two's complement gives the top bit a negative weight: in four bits the weights are 1, 2, 4 and -8. Inverting every bit of x gives -x - 1, because x plus its inverse is all ones, which is -1. Adding one then gives -x. The one value without a positive twin is the most negative one: -8 inverts to 0111, and adding one returns 1000, which is -8 again.
def to_bits(n, width):
return [(n >> i) & 1 for i in range(width)]
def from_bits(bits, signed=False):
value = sum(bit << i for i, bit in enumerate(bits))
return value - (1 << len(bits)) if signed and bits[-1] else value
x = to_bits(3, 4)
inverted = [1 - bit for bit in x]
print(inverted, from_bits(inverted, signed=True)) # -x - 1
print(from_bits(to_bits(-8, 4), signed=True), from_bits([1, 0, 0, 1], signed=True), from_bits([1, 0, 0, 1]))Subtraction needs no new hardware. Since -b is the inverted b plus one, a - b = a + ~b + 1: the same adder, with inverters on b and the carry in set to 1. A carry out means a >= b, so the borrow is 1 minus the carry out. The overflow flag is the same signed test as before.
A ripple adder is slow in a specific way: the top sum bit waits for a carry that has passed through every lower stage. With g = a AND b and p = a XOR b, each carry can be written flat as an OR of terms such as g₁ AND p₂, ending with the carry in AND every p below. That is carry lookahead. The snippet counts gate levels, assuming gates with as many inputs as needed.
def depth_after(width, lookahead):
"""Gate levels until the last carry is ready (g and p take one level)."""
if lookahead:
return 3 # g and p, one wide AND level, one wide OR level
carry = 0 # the carry in is ready at once
for _ in range(width):
and_level = max(1, carry) + 1 # p AND carry
carry = max(1, and_level) + 1 # OR with g
return carry
for width in (4, 8, 16, 32):
print(width, depth_after(width, False), depth_after(width, True))The ripple depth grows as 2n + 1 while the lookahead depth stays at 3. The price is more gates, and wider ones than real parts offer, so practical designs look ahead over groups of four bits and ripple between groups.
Four mistakes recur. Reporting the carry out as the signed overflow is wrong in both directions, as the 5 + 3 and 15 + 1 cases show. Leaving out the carry in of a subtraction makes every difference one too small, and only a test with a known difference notices. Reading a list as if its first entry were the top bit swaps the weights of the whole word. And negating the most negative value gives back the same value, which is correct behaviour for four-bit words, not a bug.
The tests compare every function with Python integers: all 512 combinations of two 4-bit words and a carry in, a sample at 8 bits, and every 8-bit negation. Passing shows that the logic is the arithmetic you meant. It does not show how fast a real circuit would be: the code evaluates instantly, so the depth counts above are a model of gate levels, not a measurement of time.