Results 1 to 3 of 3
- 06-23-2010, 10:49 PM #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:
- 06-24-2010, 12:37 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 06-24-2010, 12:07 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Streaming an image byte by byte (and similtaneosly rendering it on screen)
By ea25 in forum New To JavaReplies: 1Last Post: 04-21-2010, 02:28 AM -
get the value from a table in a month range
By javastuden in forum JDBCReplies: 1Last Post: 02-24-2010, 07:53 AM -
In-Range adder
By filem_funk in forum New To JavaReplies: 1Last Post: 11-27-2009, 01:06 AM -
Range within an Array
By End in forum New To JavaReplies: 6Last Post: 04-18-2009, 06:53 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks