Convert long to two int and vice versa

user1293081 picture user1293081 · May 21, 2012 · Viewed 13.3k times · Source

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

Answer

Fuwjax picture Fuwjax · Jun 26, 2012
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.