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
2009-11-03 02:26:13
But I heard about the wrap-around terminology? Does it use more bits in wrap-around?
tsubasa
2009-11-03 02:27:34
Isn't it still allowed in 2-complement: 1+127 = 128?
tsubasa
2009-11-03 02:32:24
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
2009-11-03 02:34:45
Actually, all bits on mean -1 in 2s complement. -127 is 10000000
Chris Dodd
2009-11-03 02:42:13
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
2009-11-03 02:49:54
A:
Loadmaster
2009-11-03 03:23:35
"treat 1000 0000 as both −128 and +128": how would you then differentiate between the two??
Amro
2009-11-03 03:36:29
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
2009-11-03 16:10:10
+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
2009-11-03 03:31:53