Compare unsigned shorts in Java

Convert to int and compare.

int cmp = Integer.compare(Short.toUnsignedInt(s1), Short.toUnsignedInt(s2));
// cmp = -1  =>  s1 < s2
// cmp =  0  =>  s1 = s2
// cmp =  1  =>  s1 > s2

Comments

Be the first to comment!