ultraq / redhorizon

Recreating the original 2D Command & Conquer games
Apache License 2.0
6 stars 4 forks source link

No more need for byte masking (val & 0xff...) ??? #13

Closed ultraq closed 4 years ago

ultraq commented 4 years ago

Because Groovy uses autoboxing behind the scenes for all of its primitive types, we don't fall into the trap in vanilla Java where going from a small primitive to a bigger one fills in the bits w/ 1s, eg:

byte b = 127; // 0xff
int i = b; // Doesn't stay as 127, but instead 0xffffffff

But in Groovy:

byte b = 127; // 0xff
int i = b; // Stays as 127, becomes 0x000000ff

This should make all the bit/byte operations that I had to mask in Java unnecessary! 🎉 Man Groovy is the best 😁

ultraq commented 4 years ago

OK, I've found there are some tricks/caveats with this. If I'm still dealing with hard primitives, like those coming out of other Java code and not my own Groovy code, then it's still subject to the usual Java behaviour. So I can't just go removing all the bit masks and call it a day - gotta either test or make sure if doesn't break anything.

ultraq commented 4 years ago

Done in all the places I thought it safe!