HiveBrain v1.2.0
Get Started
← Back to all entries
patternModerate

How does 0 have two values in one's complement?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
twoonecomplementdoeshowvalueshave

Problem

It is said that in 2's complement 0 has only one value, while in 1's complement both +0 and -0 have separate values. What are they?

Solution

In 1's complement you just invert all the bits.

Consider these 2 examples (assuming 8 bits):

-
$4 = 00000100$, so $-4= 11111011$

-
$0 = 00000000$, so $-0=11111111$.

So you have 2 ways to represent the number 0

In 2's complement you add 1 to the 1's complement representation of the negative number

  • $-4$ that in 1's complement was $11111011$ becomes $11111100$



  • $-0$ that in 1's complement was $11111111$ becomes $00000000$ same as 0



So you have just one way to represent the 0 in this case

As you can see from the examples the difference is that:

  • in 1's complement, with 8 bits you can just express numbers from $-2^{7}+1$ to $2^7-1$ (from -127 to 127)



  • in 2's complement with 8 bits you can express numbers from $-2^{7}$ to $2^{7}-1$ (from -128 to 127), so one more number

Context

StackExchange Computer Science Q#72424, answer score: 19

Revisions (0)

No revisions yet.