My Tech Notes

Search This Blog

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

  • unsigned int in java
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: develop, java

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Total Pageviews

Theme images by Naseko. Powered by Blogger.