Convert long to unsigned short in Java

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

long lng = 65000L;       // 00000000 ... 00000000 11111101 11101000
short s = (short) lng;   //                       11111101 11101000

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

Comments

Be the first to comment!