EN

Adders and subtractors

From the one-bit full adder to the 8-bit two's complement adder/subtractor

1. One-bit full adder

Click the inputs to change them. Green wires are at 1.

Truth table

2. Multi-bit adder and subtractor

Operation

Half adder and full adder

Adding two bits gives a two-digit result: 1 + 1 = 10 in binary. The half adder computes the sum S with an XOR and the carry C with an AND. To add multi-bit numbers, the carry coming from the previous column must also be added, and that is what the full adder does; it is made of two half adders and an OR:

\[ S = A \oplus B \oplus C_{in} \]
\[ C_{out} = A\,B + C_{in}\,(A \oplus B) \]

Cout is 1 when at least two of the three inputs are 1: it is the majority function. Writing it the second way, with the XOR that already exists for the sum, saves gates.

n-bit ripple-carry adder

Placing n full adders in a row, with each Cout connected to the next Cin, adds the way it is done by hand: column by column, from right to left. This is the ripple-carry adder. Its drawback is speed: the result is not valid until the carry has travelled through every stage, about two gate delays per bit. At 32 or 64 bits that is too slow, so carry look-ahead is used, which computes the carries in parallel from the generate G = A·B and propagate P = A ⊕ B signals. The 74HC283 is a 4-bit adder with internal carry look-ahead.

Subtracting with two's complement

Digital circuits do not subtract directly: they add the opposite. In two's complement, the opposite of B is obtained by inverting all its bits and adding 1. This way a single adder serves both operations: one XOR per bit inverts B when the mode signal M is 1, and that same M goes in as the initial carry to add the 1:

\[ A - B = A + \overline{B} + 1 \]
\[ B_i' = B_i \oplus M \qquad C_0 = M \]

With n bits, two's complement represents numbers from −2n−1 to 2n−1 − 1 (−8 to 7 with 4 bits, −128 to 127 with 8). The same bit pattern can be read as an unsigned number (0 to 2n − 1) or as a signed one: the adder makes no distinction, only the flags you look at change.

Carry and overflow

FlagMeaningWhen it signals an error
C (carry)Carry out of the highest bitFor unsigned numbers: in addition, C = 1 means the result does not fit; in subtraction, C = 0 means there was a borrow (A < B)
V (overflow)Cn ⊕ Cn−1: the two highest carries differFor signed numbers: adding two positives gives a negative or two negatives give a positive
Z (zero)All result bits are 0—
N (negative)Highest bit of the result—

These are the same flags as in a microcontroller's status register, and conditional jumps are built on them: after subtracting two numbers, Z = 1 means they are equal and C tells, unsigned, which one is greater. Try 7 + 1 with 4 bits in the calculator: unsigned it gives 8, which is correct, but signed it gives −8 and V is set.

Adding in BCD

In BCD each decimal digit takes 4 bits. When two digits are added with a binary adder, the result is correct up to 9; if it exceeds 9 or there is a carry, 6 (0110) is added to skip the six combinations BCD does not use, and the carry to the next digit is generated. For example, 7 + 5 = 1100 in binary; adding 6 gives 1 0010, that is, 12 in BCD.

ICFunction
74HC2834-bit binary adder with carry look-ahead
CD40084-bit adder from the 4000 series
74HC86Four XORs, to turn the adder into an adder/subtractor
74LS1814-bit arithmetic logic unit, see 4-bit ALU

Related: magnitude comparators, 4-bit ALU, number system converter and logic gate simulator (the full adder is one of its examples).