Odd behavior when Java converts int to byte?
int i =132;
byte b =(byte)i; System.out.println(b);
Mindboggling. Why is the output -124
?
In Java, an int
is 32 bits. A byte
is 8 bits
.
Most primitive types in Java are signed, and byte
, short
, int
, and long
are encoded in two's complement. (The char
type is unsigned, and the concept of a sign is not applicable to boolean
.)
In this number scheme the most significant bit specifies the sign of the number. If more bits are needed, the most significant bit ("MSB") is simply copied to the new MSB.
So if you have byte 255
: 11111111
and you want to represent it as an int
(32 bits) you simply copy the 1 to the left 24 times.
Now, one way to read a negative two's complement number is to start with the least significant bit, move left until you find the first 1, then invert every bit afterwards. The resulting number is the positive version of that number
For example: 11111111
goes to 00000001
= -1
. This is what Java will display as the value.
What you probably want to do is know the unsigned value of the byte.
You can accomplish this with a bitmask that deletes everything but the least significant 8 bits. (0xff)
So:
byte signedByte = -1;
int unsignedByte = signedByte & (0xff);
System.out.println("Signed: " + signedByte + " Unsigned: " + unsignedByte);
Would print out: "Signed: -1 Unsigned: 255"
What's actually happening here?
We are using bitwise AND to mask all of the extraneous sign bits (the 1's to the left of the least significant 8 bits.) When an int is converted into a byte, Java chops-off the left-most 24 bits
1111111111111111111111111010101
&
0000000000000000000000001111111
=
0000000000000000000000001010101
Since the 32nd bit is now the sign bit instead of the 8th bit (and we set the sign bit to 0 which is positive), the original 8 bits from the byte are read by Java as a positive value.
132
in digits (base 10) is 1000_0100
in bits (base 2) and Java stores int
in 32 bits:
0000_0000_0000_0000_0000_0000_1000_0100
Algorithm for int-to-byte is left-truncate; Algorithm for System.out.println
is two's-complement (Two's-complement is if leftmost bit is 1
, interpret as negative one's-complement (invert bits) minus-one.); Thus System.out.println(int-to-byte(
))
is:
- interpret-as( if-leftmost-bit-is-1[ negative(invert-bits(minus-one(] left-truncate(
0000_0000_0000_0000_0000_0000_1000_0100
) [)))] ) - =interpret-as( if-leftmost-bit-is-1[ negative(invert-bits(minus-one(]
1000_0100
[)))] ) - =interpret-as(negative(invert-bits(minus-one(
1000_0100
)))) - =interpret-as(negative(invert-bits(
1000_0011
))) - =interpret-as(negative(
0111_1100
)) - =interpret-as(negative(124))
- =interpret-as(-124)
- =-124 Tada!!!
byte in Java is signed, so it has a range -2^7 to 2^7-1 - ie, -128 to 127. Since 132 is above 127, you end up wrapping around to 132-256=-124. That is, essentially 256 (2^8) is added or subtracted until it falls into range.
For more information, you may want to read up on two's complement.
132 is outside the range of a byte which is -128 to 127 (Byte.MIN_VALUE to Byte.MAX_VALUE) Instead the top bit of the 8-bit value is treated as the signed which indicates it is negative in this case. So the number is 132 - 256 = -124.