In Java, numeric promotions happen when two operands of an arithmetic expression have different sizes. More specifically, narrower operands get
promoted to the type of wider operands. For instance, an operation between a byte
and an int
, will trigger a promotion of
the byte
operand, converting it into an int
.
When this happens, the sequence of 8 bits that represents the byte
will need to be extended to match the 32-bit long sequence that
represents the int
operand. Since Java uses two’s complement notation for signed number types, the promotion will fill the missing
leading bits with zeros or ones, depending on the sign of the value. For instance, the byte 0b1000_0000
(equal to -128
in
decimal notation), when promoted to int
, will become 0b1111_1111_1111_1111_1111_1111_1000_0000
.
When performing shifting or bitwise operations without considering that bytes are signed, the bits added during the promotion may have unexpected
effects on the final result of the operations.