Unsigned Int in Java

cdecker picture cdecker · Dec 15, 2010 · Viewed 23.3k times · Source

I'm trying to implement an existing network protocol which makes heavy use of Unsigned datatypes, which are not supported by Java. What I currently do is for each datatype, chose the next bigger one so that the unsigned number may fit into the positive region and then use byte shifting to get the desired effect. Since this is pretty error prone and for an unsigned long onward I have to use BigInteger which is a lot heavier than expanded types, I was wondering if there isn't a better way to achieve this?

Answer

Peter Lawrey picture Peter Lawrey · Dec 15, 2010

Depending on what you are doing, you can just treat long as a 64-bit value and int as a 32-bit value. Most operations esp readInt/Long writeInt/Long work just the same by ignoring the signness.

Can you give an example of operation you perform on these numbers and perhaps we can suggest how would do the same thing without having to expand the type.

For example, ++, --, +, -, *, ==, !=, << all work the same regardless of signess (i.e. give the same answer). for >> you can substitue >>>

It is the /, %, >, >=, <, <= and printing functions which assume signed values, but you should be able to work around these (if you use these).

e.g.

long unsignedA = 
long unsignedB = 
boolean greater = unsignedA + Long.MIN_VALUE > unsignedB + Long.MIN_VALUE

EDIT: Why does this work? Partly because java doesn't have overflow/underflow exceptions.

e.g.

byte unsignedA = 0;
unsignedA--; 
// unsignedA == FF, is this -1 or 255? Java assumes the former but you assume the later

byte unsignedB = unsignedA * unsignedA;
// unsignedB is -1 * -1 = 1 or (byte) (255*255) = (byte) 65525 = 1.