tags:

views:

81

answers:

2

OK, this is as noob as it gets, but I still don't get why the lowest value a byte can take is -128. That the highest value is 127 I can understand, because it's 01111111 in binary, but how does one represent -128 with only 8 bit, one of which is used for the sign? Positive 128 would already be 8-bit, i.e. 10000000, and then you need a 9th bit to represent the negative sign. What did I miss here?

+9  A: 

The answer is two's complement.

In short, Java (and most modern languages) do not represent signed integers using signed-magnitude representation. In other words, an 8-bit integer is not a sign bit followed by a 7-bit unsigned integer.

Instead, negative integers are represented in a system called two's complement, which allows easier arithmetic processing in hardware, and also eliminates the potential ambiguity of having positive zero and negative zero. A side effect of eliminating negative zero is that there is always one extra negative number available at the bottom of the range.

Another interesting property of two's complement systems is that the first bit does effectively function as a sign indicator (i.e. all numbers beginning with the bit 1 are negative), but the next seven bits are not to be interpreted on their own as an unsigned number to which the sign bit is applied.

Two's complement isn't terribly complicated, but getting an initial good grip on what two's complement is and how and why it works is probably beyond the scope of an SO answer. Start with the Wikipedia article, or google the term for more resources.

To try to briefly address your query about -128, the fundamental idea behind generating a two's complement number is to take the unsigned form of the number, invert all of the bits and add one. So unsigned 128 is 10000000. Inverted, it's 01111111, and adding one gets 10000000 again. So in a two's complement system, 10000000 is unambiguously -128 and not +128. Numbers greater than or equal to +128 simply cannot be represented in 8 bits using a two's complement system because they would be ambiguous with the forms of negative numbers.

Tyler McHenry
Neat! Let's see if I got it right though: it is still true that 8-bit numbers that start with 0 (other than 00000000) ARE positive, and starting with 1 ARE negative? Also, the only really tricky thing about two's complement is that the bit representation of bytes don't have the same value that they do in a math class, i.e. 10000000 is normally +128, but as a byte it's -128. Amirite?
Anita
You're correct. The first bit is 1 if and only if the number is negative. If the first bit is 0, the number is either positive or zero.
Tyler McHenry
+1  A: 

As James pointed out in his comment, it's because that's how two's complement works.

If we put it in other terms, you can represent 2^8 = 256 kind of values. which is, in this case used as 128 negative numbers, 127 positive numbers, and zero. If we used 7 bits to represent the value, +1 bit for a sign, we could represent one less value and would also have two zeros (which would be very unfortunate as comparing two values would be more complicated because of that).

Tamás Szelei