Binary Subtraction with 2's Complement

66,714

1) -9 - 7

-9 - 7 = -9 + -7

9 (binary) = 01001
-9 (2's complement) = 10111
7 (binary) = 00111
-7 (2's complement) = 11001

 10111 +
 11001 =
110000

This doesn't fit into 5 bits. Removing the overflow we get 10000, which is -16 (binary).

2) 6 - 10

6 - 10 = 6 + -10

6 (binary) = 00110
10 (binary) = 01010
-10 (2's complement) = 10110

 00110 +
 10110 =
 11100

This fits into 5 bits and is -4 (binary).

Share:
66,714
iOSDev
Author by

iOSDev

Updated on January 18, 2020

Comments

  • iOSDev
    iOSDev over 4 years

    I need help subtracting with binary using 2's representation and using 5 bits for each number:

    1) -9 -7 = ? Is there overflow?

    -9 = 01001 (2's complement = 10111) and -7 = 00111 (2's complement = 11001)

    Now we need to add because we're using 2's complement

    10111 +11001 = 100000 But this answer doesn't make sense. Also, I'm assuming there's overflow because there are more than 5 bits in the answer.

    2) 6 - 10, same process as before. Negative binary numbers don't make sense to me

  • Weishi Z
    Weishi Z about 7 years
    I understand computer store negative numbers by their 2's complement. I'm wondering what if 6 - a, where a == -10. How does this process of -(-10) happen? Does it try to get the 2's complement of -10 (10110) ?
  • melpomene
    melpomene about 7 years
    @WeishiZeng I don't know how real processors do it. I like to transform x - y into x + -y because then I don't need to remember a separate algorithm for subtracting binary numbers. If you have 00110 - 10110, that becomes 00110 + -10110, 00110 + 01010, 10000, which is -16. So yes, I simply take the 2's complement of 10110 by flipping all bits and adding 1.