Adders & Subtractors
How digital hardware performs arithmetic — from adding two single bits to subtracting multi-bit numbers using two's complement.
A processor adds millions of numbers per second — but at the gate level, every one of those additions starts with a circuit that can barely handle the number 1 + 1.
The half-adder: adding two bits
The simplest arithmetic circuit adds two single bits A and B and produces two outputs: a sum bit S and a carry bit C. The sum is 1 when exactly one input is 1 (the XOR), and the carry is 1 only when both inputs are 1 (the AND).
| A | B | S (Sum) | C (Carry) |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
This circuit is called a half-adder because it has no input for a carry coming in from a lower bit position. It works for the least-significant bit, but every higher position needs to account for a possible carry from below.
The full-adder: handling carry-in
A full-adder accepts three inputs: the two operand bits A and B, plus a carry-in C_in from the previous stage. The sum is the XOR of all three inputs — it is 1 when an odd number of inputs are 1. The carry-out is the majority function: it is 1 whenever two or more of the three inputs are 1.
- Sum = A ⊕ B ⊕ Cin = 1 ⊕ 1 ⊕ 1 = 0 ⊕ 1 = 1.
- Cout = AB + Cin(A ⊕ B) = (1·1) + 1·(1 ⊕ 1) = 1 + 1·0 = 1 + 0 = 1.
- The result bits are Cout=1, S=1, giving binary '11' = 3, which matches 1 + 1 + 1 = 3.
Ripple-carry adder: cascading for multi-bit addition
To add two n-bit numbers, we chain n full-adders together. The carry-out of each stage feeds directly into the carry-in of the next. The least-significant stage takes Cin = 0 (or a dedicated initial carry). Because the carry ripples leftward through each stage in sequence, this architecture is called a ripple-carry adder. It is simple to build but slow for wide words — the final sum is not valid until the carry has propagated through every stage.
- Stage 0 (bit 0): A0=1, B0=1, Cin=0 → S0 = 1⊕1⊕0 = 0, carry C1 = 1.
- Stage 1 (bit 1): A1=0, B1=1, Cin=1 → S1 = 0⊕1⊕1 = 0, carry C2 = 1.
- Stage 2 (bit 2): A2=1, B2=0, Cin=1 → S2 = 1⊕0⊕1 = 0, carry C3 = 1.
- Stage 3 (bit 3): A3=0, B3=0, Cin=1 → S3 = 0⊕0⊕1 = 1, carry C4 = 0.
- Result: S3S2S1S0 = 1000 = 8, with final carry-out C4 = 0 (no overflow). This matches 5 + 3 = 8.
- 3 + 6 = 9, which fits in 4 bits (max unsigned = 15).
- LSB-first bit values: A0=1, A1=1, A2=0, A3=0 (from 0011); B0=0, B1=1, B2=1, B3=0 (from 0110).
- Stage 0: A0=1, B0=0, Cin=0 → S0 = 1⊕0⊕0 = 1, C1 = 0.
- Stage 1: A1=1, B1=1, Cin=0 → S1 = 1⊕1⊕0 = 0, C2 = 1.
- Stage 2: A2=0, B2=1, Cin=1 → S2 = 0⊕1⊕1 = 0, C3 = 1.
- Stage 3: A3=0, B3=0, Cin=1 → S3 = 0⊕0⊕1 = 1, C4 = 0.
- Sum = S3S2S1S0 = 1001 = 9, carry-out = 0.
Subtraction via two's complement
Subtraction in digital hardware is typically performed by adding the two's complement of the subtrahend. For an n-bit number B, the two's complement is ~B + 1, where ~B inverts every bit. Then A − B = A + (~B + 1), and any carry-out beyond the n-bit width is simply discarded. This elegant trick means the same adder circuit handles both addition and subtraction — the only extra hardware is a bank of inverters and a way to feed Cin = 1 into the LSB stage.
- Invert B: ~B = ~0011 = 1100.
- Add 1: ~B + 1 = 1100 + 1 = 1101 (this represents −3 in 4-bit two's complement).
- Add A + (~B + 1) = 0101 + 1101 = 10010 (5-bit raw result).
- Discard the carry-out beyond 4 bits: 10010 → 0010.
- Result = 0010 = 2, matching 5 − 3 = 2.
- ~B = ~0110 = 1001.
- ~B + 1 = 1001 + 1 = 1010 (= −6 in 4-bit two's complement).
- A + (~B+1) = 1010 + 1010 = 10100 (5-bit raw result).
- Discard carry-out: 10100 → 0100.
- 0100 = 4, matching 10 − 6 = 4.
Check your understanding
- A half-adder adds two bits with XOR (sum) and AND (carry) but has no carry-in input.
- A full-adder adds three inputs (A, B, Cin) with S = A⊕B⊕Cin and Cout = AB + Cin(A⊕B).
- A ripple-carry adder chains full-adders stage by stage, with each Cout feeding the next Cin.
- Subtraction is done by adding the two's complement (~B + 1) and discarding any carry-out beyond the bit width.