harshit - 8 months ago 48

Java Question

`int i =132;`

byte b =(byte)i;

System.out.println(b);

The output is -124

Why is that? I know this is a very basic question, but I'm still not able to map it, or understand how this happens?

Answer

In Java, an `int`

is 32 bits. A `byte`

is 8 `bits`

.

Everything in Java is signed, and `byte`

s, `int`

s, `long`

s are encoded in two's complement.

In this numberscheme 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.