Convert long to unsigned int in Java

Casting to int throws away all but the lowest 32 bits.

long lng = 2200000000L;  // 00000000 ... 00000000 10000011 00100001 01010110 00000000
int i = (int) lng;       //                       10000011 00100001 01010110 00000000

System.out.println(i);                            // -2094967296
System.out.println(Integer.toUnsignedString(i));  // 2200000000

Comments

Be the first to comment!