2
$\begingroup$

I'm studying Computer science and this has confused me for a long time since our professor didn't give any proof.

When changing from 2's complement to the positive value, we can go in reverse (by subtracting 1, then using 1's complement), and that's clear why it works.

But our professor told us another method which is taking the number, using 1's complement, THEN adding 1.

I don't understand why the second method works.

$\endgroup$
2
$\begingroup$

Suppose that your integers are $n$ bit long. One's complement changes $x$ to $2^n-1-x$, since adding $x$ to its one's complement gives $\underbrace{1\dots 1}_{n}{}_2 = 2^n-1$.

Subtracting 1 and taking one's complement changes $x$ to $2^n-1-(x-1) = 2^n-x$.

Taking one's complement and adding 1 changes $x$ to $(2^n-1-x)+1 = 2^n-x$.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for you answer, just a question, is the sign bit (the one on the very left) included in the bit length (n) you mentioned. $\endgroup$ – Tarmius Oct 5 '19 at 23:29
  • $\begingroup$ What do you think? What would make sense given the definition of one's complement that you know and my formula? $\endgroup$ – Yuval Filmus Oct 5 '19 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.