Java – Convert long to two int and vice versa

intjavalong-integer

How can I convert two 32 bit integers (int) to one 64 bit long and vice versa?

Best Answer

long c = (long)a << 32 | b & 0xFFFFFFFFL;
int aBack = (int)(c >> 32);
int bBack = (int)c;

In Java, you don't need quite so many parentheses, or any masking on the reverse calculation.