Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I get unsigned ints in Java?

ints Java unsigned
0
Posted

How do I get unsigned ints in Java?

0

[*] Java doesn’t have unsigned ints. The reason is that this is a poorly designed area of C. The rules for what type you end up with when you mix signed and unsigned in expressions are complicated, and they changed between K&R and ANSI C (you might have heard this under the name “unsigned preserving vs. value preserving”). Worse, they depended on the underlying hardware, so they varied from platform to platform, causing bugs in all kinds of unexpected places. The book “Expert C Programming” goes into this in more depth (page 25). So, to avoid bringing over the hidden complexities, Java does not bring over unsigned types from C. Use type char if you are OK with 16-bit unsigned quantities. Otherwise, go to the next larger type and use masking. If you don’t have to worry about division by numbers with the high bit set, then when you use >>> for right shift and remember to AND each byte with 0xFF where they are being expanded into a larger type, then there is no difference in the bit patte

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123