I want to store two ints in a long (instead of having to create a new Point
object every time).
Currently, I tried this. It's not working, but I don't know what is wrong with it:
// x and y are ints
long l = x;
l = (l << 32) | y;
And I'm getting the int values like so:
x = (int) l >> 32;
y = (int) l & 0xffffffff;
y
is getting sign-extended in the first snippet, which would overwritex
with-1
whenevery < 0
.In the second snippet, the cast to
int
is done before the shift, sox
actually gets the value ofy
.Here is another option which uses a bytebuffer instead of bitwise operators. Speed-wise, it is slower, about 1/5 the speed, but it is much easier to see what is happening: