i2b

Convert int to byte

Bytecode

Type Description
u1 i2b opcode = 0x91 (145)

Stack ..., value => ..., result

Description

Converts an integer to a signed byte. A 32-bit int is popped off the stack, the top 24 bits are discarded (they are set to zero), then the resulting value is signed extended to an int. The int result is pushed back onto the stack.

i2b is used in Java where there is a cast between an int and a byte. Notice that i2b can cause a change in sign. For example, in the code:

int x = -134;
byte b = (byte)x;

The value of b is positive 122 - the sign bit of x is lost in the conversion.

The value on the top of the operand stack must be of type int. It is popped from the operand stack, truncated to a byte, then sign-extended to an int result. The result is pushed onto the operand stack.

Notes

The i2b instruction performs a narrowing primitive conversion. It may lose information about the overall magnitude of value. The result may also not have the same sign as value.