Search code examples
binarysubtractiontwos-complement

Binary Subtraction with 2's Complement


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


Solution

  • 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).