Search code examples
binarytwos-complementnumber-systems

subtracting a negative 2's complement


I have two 2's complement signed binary numbers and want to subtraction them (assuming an 8-bit subtractor)

1001 0110

1000 0001

what would be the operation to find the difference and why/whynot is there an overflow


Solution

  • to get the difference between 2 numbers you have to subtract them:
    1001 0110b = -106
    1000 0001b = -127
    so -106 - (-127) = 21

    you can also do that in binary:

     1001 0110b
    -1000 0001b
         =
     0001 0101b
    

    another method is: a-b = a+(-b), so you have to make the 2's complement of the second number. So 1000 0001b becomes 0111 1111b.

      1001 0110b
     +0111 1111b
          =
    1 0001 0101b
    

    the first 1 doesn't count because it's a 8bit number, so the result is also 0001 0101b which is 21 in decimal.