0

Whenever I read about converting an integer to its twos complement representation, I see the algorithm expressed like so:

twoscomplement(x) = ~x + 1

However, I ran into one source that defines it like so:

twoscomplement(x) = ~(x + 111...111)

Where 111...111 is the representation of -1. How can I prove to myself that this second definition is correct? Can I derive it from the first?

kaerimasu
  • 690
  • 3
  • 17
  • I can make the leap from one to the other if I can show that `~(a + b) = ~a + ~b + 1`. Then we have this: `~(x + 111...111) = ~x + ~111...111 + 1 = ~x + 0 + 1 = ~x + 1`. – kaerimasu Oct 02 '16 at 16:39
  • See the answer to [this](http://stackoverflow.com/questions/33566989/explain-why-x-x-1-1-twos-complement-and-back) question, specifically the proof that `~x + 1 == ~(x - 1)`. 2s complement negates an integer, so if you bitwise not then add one, you can reverse it by subtracting one then bitwise not. It's just the operation in reverse order. Both have the effect of negating the value. – samgak Oct 03 '16 at 23:27

0 Answers0