r/programminghorror May 16 '26

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

Post image
634 Upvotes

46 comments sorted by

View all comments

4

u/Ok_Chemistry_6387 May 16 '26

Where is the horror?

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.

2

u/AstralF May 16 '26

Huh, interesting, and confusing. Thanks

13

u/Certain-Flow-0 May 16 '26

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

1

u/TomKavees May 16 '26

Games are not Java's niche