Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd behavior when Java converts int to byte?

Tags:

java

int i =132;

byte b =(byte)i; System.out.println(b);

Mindboggling. Why is the output -124?

like image 636
harshit Avatar asked Oct 17 '22 05:10

harshit


People also ask

What happens when we convert int to byte in Java?

Int to Byte Conversion in Java In this example, we convert int to byte by casting and see if the int value exceeds out of byte's range. If it does, then it's converted into a negative value. It means if a value is larger than the byte range, then it gets converted to negative.

What happens when int is converted into bytes?

The byteValue() method of Integer class of java. lang package converts the given Integer into a byte after a narrowing primitive conversion and returns it (value of integer object as a byte). Also, remember this method does override byteValue() method of the Number class.

Can int be converted to byte?

It's not possible. A byte is 0 to 255. An int is a whole lot bigger than that. So, you can convert an int to a stream of bytes, but not to a byte.

Can we assign int to byte in Java?

Unfortunately bytes in Java are signed. All you can do is try a larger data type or a custom class.


1 Answers

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.

like image 90
Wayne Avatar answered Oct 24 '22 20:10

Wayne