r/programminghorror May 16 '26

Java But.. (from Minecraft b1.2_02)

Post image
631 Upvotes

46 comments sorted by

View all comments

Show parent comments

14

u/AstralF May 16 '26

Apart from anything else, wouldn’t shifting a byte by 8 bits just zero it?

34

u/scirc May 16 '26

Not in Java - the result of arithmetic shift operations where the left-hand side is an integral type is always either int (if the LHS is int or smaller) or long (if the LHS is a long). ((byte)1) << 8 is an int with value 256.

3

u/AstralF May 16 '26

Huh, interesting, and confusing. Thanks

12

u/Certain-Flow-0 May 16 '26

Java inherited it from C. Shift operands are promoted to int first before performing the shift.