Skip to content
Advertisement

Java storing two ints in a long

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:

JavaScript

And I’m getting the int values like so:

JavaScript

Advertisement

Answer

y is getting sign-extended in the first snippet, which would overwrite x with -1 whenever y < 0.

In the second snippet, the cast to int is done before the shift, so x actually gets the value of y.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement