Java: unsigned numbers








An int is always a signed, 32-bit number in Java. However, this only matters if you are doing math with it. If all you care about is the pattern of 0 and 1 bits, simply ignore the sign.

If you do need to do some math, convert it to a long by masking:

int u1 = ...;
int u2 = ...;
long l1 = u1 & 0xFFFFFFFFL;
long l2 = u2 * 0xFFFFFFFFL;
if (l1>l2){
   System.out.println(Long.toString(l1) + ">" + Long.toString(l2));
}


Do all arithmetic with long operands, modulo 0xFFFFFFFFL. When you are done, cast the result back to an int and transmit it.

SEE ALSO