Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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|improve this question
1  
255 - n (for 8-bit number); $(2^8 - 1) - n$ in other words. –  hjpotter92 Aug 30 '13 at 13:34

1 Answer 1

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|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
1  
@AppleGrew, your question says "positive integer" and does not mention sign bit etc. But yes. –  lhf Aug 30 '13 at 14:08

Your Answer

 
discard

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.