Easy Subtraction using 1's and 2's complement (explained)

Binary operations

Subtraction Using 1's complement

A simple algorithm to subtract two binary numbers - one's complement method

  1. Suppose a and b are the two binary numbers and we are to calculate a-b
  2. Add zero's before a or b so that both have the same number of digits
  3. Now add the sign bit to the left most position
    • add zero if the number is positive
    • add one if the number is negative
  4. Find the one's complement of b (because b is being subtracted from a)
    • invert all the digits - all 0 become 1; all the 1 become 0. 
    • You can see that the sign bit is also inverted.
  5. Add 'a' and the complement of b' (a - b = a + b'). **(b' is the complement of b.)
  6. check the value of end carry (the carry-over after the addition of the leftmost digit)
    • if the end carry  = 1, add 1 to the result.
    • if the end carry  = 0, find the complement of the result.

Subtraction Using 2's complement

A simple algorithm to subtract two binary numbers - two's complement method

  1. Suppose a and b are the two binary numbers and we are to calculate a-b
  2. Add zero's before a or b so that both have the same number of digits
  3. Now add the sign bit to the left most position
    • add zero if the number is positive
    • add one if the number is negative
  4. Find the two's complement of b (because b is being subtracted from a)
    • invert all the digits - all 0 become 1; all the 1 become 0. 
    • add 1 to this. (2's complement = 1's complement + 1)
    • You can see that the sign bit is also inverted.
  5. Add 'a' and the complement of b' (a - b = a + b'). **(b' is the complement of b.)
  6. check the value of end carry (the carry-over after the addition of the leftmost digit)
      • if the end carry  = 1, the result you've obtained is your expected result.
      • if the end carry  = 0, find the 2's complement of the result to get your expected result.

    Comments