29. Comparing two unsigned numbers

Let's consider two signed integers, Integer.MIN_VALUE (-2,147,483,648) and Integer.MAX_VALUE (2,147,483,647). Comparing these integers (signed values) will result in -2,147,483,648 being smaller than 2,147,483,647:

// resultSigned is equal to -1 indicating that
// MIN_VALUE is smaller than MAX_VALUE
int resultSigned = Integer.compare(Integer.MIN_VALUE,
Integer.MAX_VALUE);

In JDK 8, these two integers can be compared as unsigned values via the Integer.compareUnsigned() method (this is the equivalent of Integer.compare() for unsigned values). Mainly, this method ignores the notion of sign bit, and the left-most bit is considered the most significant bit. Under the unsigned values umbrella, this method returns 0 if the compared numbers are equal, a value less than 0 if the first unsigned value is smaller than the second, and a value greater than 0 if the first unsigned value is greater than the second.

The following comparison returns 1, indicating that the unsigned value of Integer.MIN_VALUE is greater than the unsigned value of Integer.MAX_VALUE:

// resultSigned is equal to 1 indicating that
// MIN_VALUE is greater than MAX_VALUE
int resultUnsigned
= Integer.compareUnsigned(Integer.MIN_VALUE, Integer.MAX_VALUE);
The compareUnsigned() method is available in the Integer and Long classes starting with JDK 8, and in the Byte and Short classes starting with JDK 9.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.184.200