How to build a 4-bit adder/subtractor from logic gates
A 4-bit adder/subtractor adds A + B when SUB = 0 and computes A - B when SUB = 1, using one ripple-carry adder: each B bit passes through an XOR with SUB (inverting it when subtracting) and SUB is also the carry-in, so the adder computes A + NOT B + 1. V flags signed overflow.
Subtraction in two's complement is adding the negative, and the negative of B is NOT B + 1. The XOR gates supply the NOT (XOR with 1 inverts, XOR with 0 passes the bit through) and the carry-in supplies the +1, so the same four full adders do both jobs. Two outputs report what happened: Cout, the carry out of the top bit, and V, the signed overflow flag, which is the XOR of the carries into and out of the top bit. All 512 input combinations are checked before the page is built.
Open this circuit in the lab →
What you need
- 4× XOR: Bi XOR SUB: invert B when subtracting
- 4× full adder: the ripple-carry adder, carry-in = SUB (each 2 XOR, 2 AND, 1 OR)
- 1× XOR: V = (carry into bit 3) XOR (carry out of bit 3)
Step by step
- Place inputs
A3toA0,B3toB0and the mode inputSUB. - XOR each B bit with SUB. With SUB = 0 the bits pass unchanged; with SUB = 1 they are inverted.
- Feed A and the XOR outputs into a 4-bit ripple-carry adder and connect SUB to its carry-in. Probe the sums as
S3toS0and the last carry asCout. - Overflow: XOR the carry into the top full adder with the carry out of it. Probe that as
V. - Try 0101 - 0011 (5 - 3): S = 0010 and Cout = 1. Then 0111 + 0001 (7 + 1): S = 1000 and V = 1, because 8 does not fit in signed 4-bit.
Examples
| SUB | A | B | Operation | S | Cout | V |
|---|---|---|---|---|---|---|
| 0 | 0101 | 0011 | 5 + 3 = 8 | 1000 | 0 | 1 |
| 0 | 0010 | 0011 | 2 + 3 = 5 | 0101 | 0 | 0 |
| 1 | 0101 | 0011 | 5 - 3 = 2 | 0010 | 1 | 0 |
| 1 | 0011 | 0101 | 3 - 5 = -2 | 1110 | 0 | 0 |
| 1 | 1000 | 0001 | -8 - 1 = -9 | 0111 | 1 | 1 |
| 0 | 1111 | 0001 | -1 + 1 = 0 | 0000 | 1 | 0 |
Read S as unsigned (0 to 15) or as signed two's complement (-8 to 7). For signed numbers, V = 1 means the true result is outside -8 to 7 (5 + 3 = 8 and -8 - 1 = -9 both overflow). For unsigned subtraction, Cout = 1 means no borrow (A was at least B). The carry versus overflow lesson explains why the two flags differ.
You can also read the full theory in the lesson on negative numbers and subtraction. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.
Build more parts from gates
Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.
Open the 4-bit adder/subtractor in the lab →Frequently asked
How does a 4-bit adder subtractor work?
A SUB control input is XORed with every bit of B and also fed into the adder's carry-in. With SUB = 0 the circuit computes A + B. With SUB = 1 it computes A + NOT B + 1, which is A - B in two's complement.
Why are XOR gates used in an adder subtractor?
An XOR with a control bit is a controllable inverter: XOR with 0 passes the bit through, XOR with 1 inverts it. That lets one control signal switch B between B and NOT B.
How is overflow detected in an adder subtractor?
Signed overflow V is the XOR of the carry into the most significant bit and the carry out of it. Equivalently, it happens when two numbers of the same sign produce a result of the opposite sign.
What does the carry out mean when subtracting?
For unsigned numbers, Cout = 1 after A - B means no borrow was needed (A >= B), and Cout = 0 means A < B. Some processors store the inverted value as a borrow flag.