tags:

views:

82

answers:

3

In 2 complements I read from wikipedia, the range is from -128 to 127. So I wonder how do we represent 128 in 2 complement as it is out of range above?

+5  A: 

You use more bits.

The range -128 to +127 is 256 unique values, which is 8 bits. If you need a larger range, you need more bits.

There is nothing restricting 2s-complement numbers to 8-bit values. For instance, a 16-bit 2s-complement number ranges from -32768 to +32767.

Mark Rushakoff
But I heard about the wrap-around terminology? Does it use more bits in wrap-around?
tsubasa
Isn't it still allowed in 2-complement: 1+127 = 128?
tsubasa
When the leftmost bit of a 2s-complement number is set, then it's a negative number. Essentially, half of the numbers you can represent with a certain number of bits are negative, and the other half are non-negative. "Wrapping around" is symmetric for any size 2s-complement binary number.
Mark Rushakoff
Actually, all bits on mean -1 in 2s complement. -127 is 10000000
Chris Dodd
Yes, you're right. Haven't thought about 2s complement for a long time, but a quick trip to the Wikipedia page reveals that you're correct.
pavium
A: 
Loadmaster
"treat 1000 0000 as both −128 and +128": how would you then differentiate between the two??
Amro
Depends on the context. If you're adding you might want to allow +128 as the max result, and conversely if you're subtracting/decrementing, you might want to allow -128 as the min. I didn't say it was necessarily a good idea, just a possibility.
Loadmaster
+1  A: 

8-bit gives you the range: [-2^7 , 2^7-1] = [-128,127]

In general, 2's complement using n-bit can represent numbers in the range:

-2^(n-1) to +2^(n-1)-1
merv