Combinational Logic Design
Adders & Subtractors
Adding numbers is the most basic thing a processor does, and it is built entirely from the logic gates you have already met. A circuit that adds two bits is called an adder, and one that subtracts them is a subtractor. Chaining these small circuits together gives you the arithmetic unit at the heart of every computer.
In this lesson you will build a half adder and a full adder from their truth tables, extend them to a 4-bit ripple-carry adder, and see how the same hardware can also subtract. You should be comfortable with binary numbers first (see the lesson on number systems) and with the XOR gate.
Learning objectives
After this lesson you will be able to:
- Derive the truth table and Boolean equations of a half adder and a full adder.
- Build a full adder from two half adders and an OR gate.
- Add two 4-bit numbers with a ripple-carry adder and trace each carry.
- Explain half and full subtractors and the meaning of the borrow output.
- Use an adder with XOR gates to subtract using two's complement.
The half adder
A half adder adds two single bits, A and B. It has two outputs: the sum and the carry that goes to the next position. Adding 1 + 1 gives binary 10, which is a sum of 0 and a carry of 1.
| A | B | Sum | Carry |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
Reading the table, the sum is 1 when exactly one input is 1, and the carry is 1 only when both are 1:
- Sum = A XOR B
- Carry = AB
So a half adder is just one XOR gate and one AND gate. It is called "half" because it has no input for a carry coming from the position on its right, so it cannot be chained.
The full adder
A full adder adds three bits: A, B and a carry-in (Cin) from the previous position. Its outputs are Sum and carry-out (Cout). The largest possible total is 1 + 1 + 1 = 3, which is binary 11, so two output bits are enough.
| 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 |
From this table:
- Sum = A XOR B XOR Cin
- Cout = AB + Cin(A XOR B), which can also be written Cout = AB + ACin + BCin
You can build a full adder from two half adders and an OR gate. The first half adder adds A and B. The second adds that result and Cin. The carries of both half adders are combined with an OR gate to give Cout. You can confirm this with the truth table generator.
Example 1: reading a full adder
Find Sum and Cout when A = 1, B = 0 and Cin = 1.
Count the 1s: there are two, so the total is 2, which is binary 10. The Sum is 0 and the Cout is 1. Check it against the equations: Sum = 1 XOR 0 XOR 1 = 0, and Cout = AB + ACin + BCin = 0 + 1 + 0 = 1.
Result: Sum = 0, Cout = 1. This matches row 6 of the truth table above.
Example 2: a 4-bit ripple-carry adder
To add numbers with several bits, connect one full adder per bit. The carry-out of each adder feeds the carry-in of the next one to its left. The first adder has Cin = 0. Because the carry has to "ripple" through the chain from right to left, this design is called a ripple-carry adder.
Add A = 1011 (decimal 11) and B = 0110 (decimal 6).
| Bit position | 3 | 2 | 1 | 0 |
|---|---|---|---|---|
| A (1011) | 1 | 0 | 1 | 1 |
| B (0110) | 0 | 1 | 1 | 0 |
| Carry in | 1 | 1 | 0 | 0 |
| Sum | 0 | 0 | 0 | 1 |
| Carry out | 1 | 1 | 1 | 0 |
Work from the right. Bit 0: 1 + 0 + 0 gives sum 1, carry 0. Bit 1: 1 + 1 + 0 gives sum 0, carry 1. Bit 2: 0 + 1 + 1 gives sum 0, carry 1. Bit 3: 1 + 0 + 1 gives sum 0, carry 1. The final carry-out becomes a fifth bit.
Result: 1011 + 0110 = 10001, which is 17 in decimal. The 4-bit sum is 0001 with Cout = 1, and 11 + 6 = 17 confirms it.
The drawback of this design is speed. Each adder has to wait for the carry from the one before it, so a wider adder is slower. Faster designs, such as the carry-lookahead adder, calculate the carries in advance.
Half and full subtractors
A half subtractor computes A - B for single bits. Its outputs are the difference and a borrow that is 1 when A is smaller than B and we have to borrow from the next position.
| A | B | Difference | Borrow |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
- Difference = A XOR B
- Borrow = A'B
A full subtractor also takes a borrow-in (Bin) from the previous position:
| A | B | Bin | Difference | Bout |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | 1 |
- Difference = A XOR B XOR Bin
- Bout = A'B + A'Bin + B Bin
Notice that the difference column is identical to the sum column of the full adder. Only the borrow differs from the carry, because A is complemented in the borrow equation.
Example 3: subtracting with an adder
In practice, computers do not need separate subtractor circuits. They use two's complement: to compute A - B, invert every bit of B, add 1, and add the result to A. The "add 1" is done by setting the first carry-in to 1. An adder-subtractor places an XOR gate in front of each B input, controlled by a signal M. When M = 0 the circuit adds, and when M = 1 each bit of B is inverted and M also feeds the first carry-in, so the circuit subtracts.
Compute 7 - 5 with 4 bits. A = 0111 and B = 0101. Inverting B gives 1010. Then 0111 + 1010 + 1 = 10010. Drop the carry-out and keep four bits: 0010.
Result: 7 - 5 = 0010, which is 2. The carry-out of 1 means no borrow was needed.
Now compute 5 - 7. A = 0101 and the inverted B is 1000. Then 0101 + 1000 + 1 = 1110, with no carry-out. A carry-out of 0 tells us the answer is negative, and 1110 is -2 in 4-bit two's complement.
Overflow. For signed numbers, an addition overflows when the carry into the leftmost bit differs from the carry out of it. This is how hardware detects results that do not fit in the available bits.
Common mistakes
- Using a half adder in the middle of a chain. Every position after the first needs a full adder, because it must accept a carry-in.
- Mixing up carry and borrow. They are not the same signal. The borrow equation uses A', and the carry equation does not.
- Forgetting the +1 in two's complement. Inverting the bits alone gives A - B - 1.
- Reading the carry-out as part of a 4-bit result. The sum of two 4-bit numbers can need 5 bits.
Practice problems
Check your answers with the Boolean calculator or the truth table generator.
-
A full adder has A = 1, B = 1 and Cin = 1. Find Sum and Cout.
Answer: Sum = 1, Cout = 1 (the total is 3, which is binary 11).
-
Add 0110 and 0011 with a 4-bit ripple-carry adder.
Answer: 1001 (6 + 3 = 9), with Cout = 0.
-
A full subtractor has A = 0, B = 1 and Bin = 1. Find the Difference and Bout.
Answer: Difference = 0, Bout = 1 (0 - 1 - 1 = -2, so we borrow 2 and the result bit is 0).
-
Compute 9 - 4 using 4-bit two's complement.
Answer: invert 0100 to get 1011, then 1001 + 1011 + 1 = 10101. Keep four bits: 0101, which is 5.
Frequently asked questions
What is the difference between a half adder and a full adder? A half adder adds two bits and has no carry-in. A full adder adds two bits plus a carry-in, so it can be chained to add numbers of any width.
Why is it called a ripple-carry adder? The carry signal passes from one full adder to the next, from the least significant bit to the most significant one. The total delay grows with the number of bits, like a ripple spreading outward.
Can one circuit both add and subtract? Yes. By passing B through XOR gates controlled by a mode signal M, and using M as the first carry-in, a single ripple-carry adder performs addition when M = 0 and subtraction when M = 1.
Next lesson: Multiplexers and Demultiplexers