Convert int to unsigned short in Java

Casting to short throws away all but the lowest 16 bits.

int i = 65000;        // 00000000 00000000 11111101 11101000
short s = (short) i;  //                   11111101 11101000

System.out.println(s);                       // -536
System.out.println(Short.toUnsignedInt(s));  // 65000

Comments

Be the first to comment!