Subtraction with Addition: Two's Complement
Subtracting numbers on paper is easy, but how does a computer do it? Most computer operations can be reduced to additions of binary numbers… even when the operation is subtraction. To understand how this works, let’s first think about how an odometer works.
Let’s say you’ve driven 7631 miles and really want to celebrate your 9999th mile and see the odometer return to 0000. You would have to drive 2368 miles to reach 9999 miles. When you’ve hit 9999 miles, the odometer returns to 0000 and the cycle continues.
And so the total amount of miles you had to drive was 2368 to hit 9999 and then 1 mile to hit 10000.
The formula to calculate the amount of miles you have to drive to hit 9999 miles is:
7631 + x = 9999 x = 9999 - 7631 x = 2368 2368 + 1 = 2369 miles driven in total for odometer reset.
The mind-blowing part of this is that 2369 is the negative equivalent of 7631. How?
Let’s pick another random 4-digit number and subtract 7631 from it:
9473 - 7631 = 1842
That's also equivalent to adding negative 7631:
9473 + (-7631) = 1842
Let’s substitute -7631 for its positive equivalent:
9473 + 2369 = 11842
11842 looks familiar doesn’t it? Since we’re operating in a 4-digit system, we drop the first digit and we have 1842, the correct result of the subtraction.
We can also use this negative representation of the number in other equations:
2354 - 7631 = -5277
So -5277 is the correct answer.
Let’s convert that answer back to the positive equivalent:
5277 - 1 = 5276 9999 - 5276 = 4723
Now… let’s see what happens when we only use addition with the positive equivalent number:
2354 + (-7631) 2354 + 2369 = 4723
🎉 Voila! Obviously, converting 4723 to its negative equivalent will result in -5277!
However! Instead of reversing the calculation like we just did, we can also covert a negative to a positive using the exact same procedure without reversing it:
9999 - 5277 = 4722 4722 - 1 = 4723
🎉 And the result is back to 4723.
This method is called the two's complement and is exactly how computers implement signed binary numbers! We just looked at base-10 numbers, but computers work in binary, base-2.
The same idea applies in binary. Take the binary number 0110.
The negative number of 0110 can be calculated in the same way, except the odometer resets at 1111 and not 9999.
1111 - 0110 = 1001 1001 + 0001 = 1010
With that, the negative number for 0110 is 1010. We can then do similar calculations, let’s say 7 minus 6.
7 - 6 = 1 (in decimal) 0111 - 0110 (in binary) 0111 + (- 0110) 0111 + 1010 = 10001, drop the first digit, resulting in 1!
Typically the computer uses 16-bit inputs, following this example means instead of 4-digit, computers use 16-digit binary numbers.
The system can code a total of 2n signed numbers, where 2n-1 - 1 and -2n-1 represent the number of positive values and negative values respectively. This means in a 4-bit system, the total number of numbers (haha!) represented is 24 = 16, where we can go up to positive 7 and as low as -8.
The two's complement is implemented in the hardware of the Arithmetic Logical Unit chip that makes up the CPU.