0

I am trying to do this equation -122 -(7) and in the result i'm getting = 01111111

I used 2's complement for -122 and got = 10000110. And I used 2's complement for 7 so I can make addition and got = 11111001. And when I am adding both I am getting = 01111111 with carry 1. I don't think this is correct answer for -129

Max
  • 1

1 Answers1

0

Ignore the Overflow Bit

Two's complement with a bit-width of 8 has a range of [-128, 127].

This binary math shows what (-122) + (-7) is equal to.

  122 =>    01111010
  7   =>    00000111

  -122 =>   10000110
  -7   =>   11111001
+         +
------    ----------
  127     1 01111111

01111111 => 127

In this example, we see that there is an overflow bit designating -256. -256 is outside of the 8-bit range of [-128, 127]. Most implementations will drop this overflow bit, and so the result would be 127.

Further Reading

There are other stackoverflow answers that delve into two's complement overflow in greater detail if you're curious.

Ross Jacobs
  • 2,962
  • 1
  • 17
  • 27