Consider the following table.
| Bits | Unsigned Decimal |
Character |
|---|---|---|
| 0111 1110 | 126 | ~ |
| 0111 1111 | 127 | |
| 1000 0000 | 128 | |
| 1000 0001 | 129 | |
| 1000 0010 | 130 | |
In this table, the bits in the first column are written using the conventional order, the most-significant bit on the left. (In Arabic numerals the "1" in "123" is called the most-significant digit. Similarly, the "3" in "123" is called the least-significant digit.) Leave the actual arrangement of bits - the way they are placed in RAM chips, the wires they use on the bus - up to the computer hardware designers. Trust that they'll order the bits from most to least significant and never get the pattern backwards.
The middle column shows the value that you would have if these bits were interpreted as a number where the least significant bit represented the value one, the next bit represents two, and so on up to the most significant bit representing one hundred and twenty-eight.
The final column shows the value that these bit patterns cause to be printed or written on your screen, in the standard ASCII-based character set. (Although the java char uses 16 bits, the values from 0x0000 through 0x00FF are the same as the 8-bit ASCII set.)
| Bits | Unsigned Decimal |
Java's Signed Integer |
|---|---|---|
| 0111 1110 | 126 | 126 |
| 0111 1111 | 127 | 127 |
| 1000 0000 | 128 | -128 |
| 1000 0001 | 129 | -127 |
| 1000 0010 | 130 | -126 |
Here the first two columns show the same bit patterns and unsigned decimal values you saw already. The new column shows the interpretation that java will use if you use a byte as a number.
Now look at the following program and try to predict its output.
public class byte-value {
public static void main( String[] args ) {
byte b = (byte) 0x80; // 1000 0000
int i1 = 0x80;
int i2 = b;
byte[] byteArray = { b };
String s = new String( byteArray );
System.out.println(
"b=" + b +
",i1=" + i1 +
",i2=" + i2 +
",s=" + s
);
} // end of main()
} // end of class byte-value
When you run this, the byte value is "-128" as you saw in the table that showed the signed integer value for the bit pattern "1000 0000". The value of the first integer is 128, since the decimal value of 0x80 is 128. However, the second integer is -128 since that is the integer value of the byte.
Finally, the String representation is the Euro currency symbol you find at location 128 in the ASCII character table. When you create a String (an array of chars) the bytes are interpreted as unsigned integers, which gives the result that you want for most file work. If you have a String, you can use string.getBytes() to get a byte array that you can write to disk. When you create a new String from that array - new String(byteArray) - you return the original String.
© 2005 by Martin Rinehart