#complement #computer #binary #number #subtraction

Complements in general means something that is opposite. We often mention complements in colours to indicate opposite colour of a given colour. In the elementary root of things, addition (+) and subtraction (-) are complement each other in some ways. As I was preparing for the binary arithmetics last week, an obvious question crossed my mind, What is arithmetic has anything to do with complement? why is complements so important for the computer operations?

The simple answer is to work on the **subtractions using the additions**. What does this mean? We basically use addition to get the subtracted result.

Let’s start with a simple example of **subtractions using the additions** in 10’s complement, 653 − 372.

- Determine how many digits would involve in doing the subtraction, in our case, we have 3.
- Find complement of 372, by subtracting from 1000, which is 628.
- Add the 1st positive with 628, which is 1281.
- Remove the overflow digit (4th) which is 1000, the answer is 281.
- Check your answer against 653 − 372 which is 281.

Although this is a very simple illustration (using decimal), the concept of complement is fairly important in computing because computers often need different hardware to do subtraction than addition. However, by using 2’s complement subtraction can be done using the addition which could save some value eventually. We use 2’s complement because we deal with binary numbers in computers at the end of the day.

I’d like to show an example of 2’s complement. Let’s assume we have a byte-sized memory (8-bit limit). Say we are subtracting 10001010 - 00101111, how do we do this using the addition?

- Get the 1’s complement which is a bitwise NOT of the subtrahend (00101111) first, which is equal to 11010000.
- Add 1 to the result = 11010001, which is the 2’s complement of the subtrahend
- Add the result with the minuend, 10001010 + 11010001 = 101011011
- Remove the overflow bit (9th) since we are dealing with 8-bit, 01011011.
- Confirming the result in decimal, 10001010 (138) - 00101111 (47) = 01011011 (91)

Things do get a little bit more interesting though. How about when we do not have any carry bit which wouldn’t have any overflow? Let’s see another example: 00000100 - 00001111

- Getting 1’s complement of 00001111, which is 11110000.
- Add 1 to the result as usual and get 11110001.
- Add the result with the minuend, 00000100 + 11110001
- Gives the result of 11110101 (still 8-bit). When there is no carry, the first bit is probably 1, meaning its a
**negative**value. - To find out the value of the negative, we need to take 2’s complement of 11110101 which is 00001011 (00001010 + 1).
- Check the result which is -00001011 which is same as -11. You can always check your answer using the decimal.

Let’s even twist the problem further by adding few decimal places to the binary number. How about this? 10100.01 – 11011.10

- 2’s complement of 11011.10 (Subtrahend) is 00100.10
- Add with minuend, 10100.01 + 00100.10 = 11000.11
- Again, there is no carry bit (overflow). so the result of the subtraction is
**negative** - The answer is obtained by writing the 2’s complement of 11000.11 which is –00111.01.
- To check answer in decimal, 10100.01 (20.1) – 11011.10 (27.2) = –00111.01 (-7.1)