Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Of course we can do that by converting the number to binary and then converting it back to decimal, but to do that directly in decimal?

share|cite|improve this question
255 - n (for 8-bit number); $(2^8 - 1) - n$ in other words. – hjpotter92 Aug 30 '13 at 13:34
up vote 1 down vote accepted

You need to fix a maximum number of bits $b$ and then the one's complement of a positive integer $x$ with at most $b$ bits is given by $x'=2^b-1-x$, because by definition the sum $x+x'$ has all bits set and hence is of the form $2^b-1$.

If you do not fix a maximum number of bits then you can still flip all significant bits, but then the map $x\mapsto x'$ is no longer injective (and hence not invertible) because for instance $0$ and $2$ are mapped to $1$.

share|cite|improve this answer
I think it should be $2^{b+1}-1$ if I assume MSB is not reserved for sign bit. – AppleGrew Aug 30 '13 at 14:07
@AppleGrew, your question says "positive integer" and does not mention sign bit etc. But yes. – lhf Aug 30 '13 at 14:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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