Computer Arithmetic

binary subtraction

Here is a small magic trick the hardware plays: it never really subtracts. Instead of building a separate borrow-based subtractor, the ALU computes a minus b by adding a to the negative of b. It reuses the very same adder that does addition. The clever part is how it negates b cheaply, and the answer is two's complement: flip every bit of b and add 1, and you have minus b in the same number of bits.

Walk it through. To compute a minus b, the ALU sends a to one adder input and the bit-flipped b to the other, then sets the adder's carry-in to 1 — that incoming 1 is the 'add 1' step of negation, so it costs nothing extra. The adder does a + (NOT b) + 1, which equals a + (minus b) = a minus b. Example in 4 bits: 7 minus 5. b = 0b0101, flip to 0b1010, set carry-in 1. Add 0b0111 + 0b1010 + 1 = 0b0010 = 2. Correct. A single control wire (the carry-in) and a row of inverters on b turn an adder into an adder-subtractor.

This is the deep reason two's complement is everywhere. Because subtraction is just addition of a negated operand, one piece of hardware serves both, and there is exactly one representation of zero (no separate plus-zero and minus-zero to special-case). The honest catch is the same as for addition: the result must fit in the word width. Subtracting can overflow too — for instance, a large positive minus a large negative can exceed the positive range — and the hardware signals that through the condition flags rather than silently giving nonsense.

Compute 7 minus 5 in 4 bits. Negate 5: flip 0b0101 to 0b1010, then add 1 via carry-in. The adder does 0b0111 + 0b1010 + 1 = 0b0010 = 2, using the addition hardware unchanged.

Subtraction = add the inverted operand with carry-in set to 1. No borrow circuit needed.

Subtraction can overflow just like addition. A common surprise: negating the most-negative number (e.g. minus 128 in 8 bits) gives back the same value, because there is no positive 128 to land on.

Also called
二進位相減