Complement

Why is this important?


24 Feb 2018 View Comments
#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.

  1. Determine how many digits would involve in doing the subtraction, in our case, we have 3.
  2. Find complement of 372, by subtracting from 1000, which is 628.
  3. Add the 1st positive with 628, which is 1281.
  4. Remove the overflow digit (4th) which is 1000, the answer is 281.
  5. 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?

  1. Get the 1’s complement which is a bitwise NOT of the subtrahend (00101111) first, which is equal to 11010000.
  2. Add 1 to the result = 11010001, which is the 2’s complement of the subtrahend
  3. Add the result with the minuend, 10001010 + 11010001 = 101011011
  4. Remove the overflow bit (9th) since we are dealing with 8-bit, 01011011.
  5. 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

  1. Getting 1’s complement of 00001111, which is 11110000.
  2. Add 1 to the result as usual and get 11110001.
  3. Add the result with the minuend, 00000100 + 11110001
  4. Gives the result of 11110101 (still 8-bit). When there is no carry, the first bit is probably 1, meaning its a negative value.
  5. To find out the value of the negative, we need to take 2’s complement of 11110101 which is 00001011 (00001010 + 1).
  6. 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

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

Me

I am a passionate programmer working in Vancouver. I strongly believe in art of algorithms and together with it to write clean and efficient software to build awesome products. If you would like to connect with me, choose one from below options :) You can also send me an email at