# Thread: pls help in understanding the range of byte

1. Member
Join Date
Jun 2010
Posts
2
Rep Power
0

## pls help in understanding the range of byte

the range of byte according to book is -128 to 127. The binary representation of -128 is 110,000,000 which is 9 bits so how it can be stored in a byte. When I convert -128 to its binary form and print it to console the output is 10,000,000 that is 0x80 in hex. But the compiler gives an error when I execute the following statement byte b=0x80. The error it gives is possible loss of precision. Pls help am confused.:confused:

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
You say that the binary representation of -128 is 110 000 000, but I imagine there are lots of binary representations of numbers.

The JLS describes bytes as 8 bit signed two's-complement integers. The JLS link is perhaps not so useful at this stage, but you will probably want to check out the details of two's complement. (Eg at Wikipedia). According to the table on that page the representation of -128 is 10 000 000 which is exactly what you saw when you used System.out.print*().

That you got a complaint from the compiler when you said "byte b = 0x80" is no surprise: after all you said the range of byte is [-128,127] and 0x80 is 128. The same bit pattern may well be used for the byte value of negative 128, but the compiler will treat "0x80" as an integer with a value of 128 and that's too big.

3. Member
Join Date
Jun 2010
Posts
2
Rep Power
0
thanks......understood it from ur wiki link

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•