2

I can store numbers ranging from -127 to 127 but other than that it is impossible and the compiler give warning. The binary value of 127 is 01111111, and 130 is 10000010 still the same size (8 bits) and what I think is I can store 130 in Byte but it is not possible. How did that happen?

5gon12eder
  • 21,864
  • 5
  • 40
  • 85
Tesfa Zelalem
  • 599
  • 1
  • 7
  • 18
  • Java uses [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation of _signed_ integers (as well as bytes, longs and shorts) – Alex Shesterov Jan 17 '15 at 12:56
  • 2
    possible duplicate of [Why is the range of bytes -128 to 127 in Java?](http://stackoverflow.com/questions/3621067/why-is-the-range-of-bytes-128-to-127-in-java) – Andy Brown Jan 17 '15 at 13:04

3 Answers3

5

Java does not have unsigned types, each numeric type in Java is signed (except char but it is not meant for representing numbers but unicode characters).

Let's take a look at byte. It is one byte which is 8 bits. If it would be unsigned, yes, its range would be 0..255.

But if it is signed, it takes 1 bit of information to store the sign (2 possible values: + or -), which leaves us 7 bits to store the numeric (absolute) value. Range of 7 bit information is 0..127.

Note that the representation of signed integer numbers use the 2's complement number format in most languages, Java included.

Note: The range of Java's byte type is actually -128..127. The range -127..127 only contains 255 numbers (not 256 which is the number of all combinations of 8 bits).

icza
  • 289,344
  • 42
  • 658
  • 630
  • @MarkoTopolnik You're right, included it in my answer as an exception. – icza Jan 17 '15 at 13:29
  • Note that I actually use `char` as an `unsigned short` with success in a project. It is better to view `char` as having the *additional* property of representing Unicode characters. – Marko Topolnik Jan 17 '15 at 13:31
  • @MarkoTopolnik Yes, you might use `char` for numbers but that might cause confusion and a less readable source code. If I see a variable of type `char`, I immediately associate to that: a unicode character. – icza Jan 17 '15 at 13:35
  • Well, if you truly need that range and you have tight space concerns, then using `short` and forcing all the arithmetic to work with it as if it were unsigned definitely results in much worse code. Confusion is a fair point generally, but when this is something consistently used in a project, it doesn't take long to get accustomed. – Marko Topolnik Jan 17 '15 at 13:41
2

In Java, a byte is a signed data type. You are thinking about unsigned bytes, in which case it is possible to store the value 130 in 8 bits. But with a signed data type, that also allows negative numbers, the first bit is necessary to indicate a negative number.

There are two ways to store negative numbers, (one's complement and two's complement) but the most popular one is two's complement. The benefit of it is that for two's complement, most arithmetic operations do not need to take the sign of the number into account; they can work regardless of the sign.

Erwin Bolwidt
  • 28,093
  • 15
  • 46
  • 70
1

The first bit indicates the sign of the number: When the first bit is 1, then the number is negative. When the first bit is 0, then the number is positive. So you basically only have 7 bits available to store the magnitude of the number. (Using a small trick, this magnitude is shifted by 1 for negative numbers - otherwise, there would be two different bit patterns for "zero", namely 00000000 and 10000000).

When you want to store a number like 130, whose binary representation is 10000010, then it will be interpreted as a negative number, due to the first bit being 1.

Also see http://en.wikipedia.org/wiki/Two%27s_complement , where the trick of how the magnitude is shifted is explained in more detail.

Marco13
  • 50,927
  • 9
  • 71
  • 148
  • While it is true that numbers represented in two's complement have the most significant bit set if and only if they are negative, the explanation using “sign” and “magnitude” is misleading. The most negative number a byte can represent, –128, has the bit pattern `10000000` but clearly, it's magnitude is not zero. – 5gon12eder Jan 17 '15 at 13:05
  • @5gon12eder I tried to clarify this a little. It's difficult to write this in a way that is easy enough to understand and still be absolutely precise (at least, without diving into the depths of Two`s complement and replicating the whole wikipedia article here...) – Marco13 Jan 17 '15 at 18:21