Home Java Integer Methods compareUnsigned() Java Integer compareUnsigned() Method Definition In Java, Integer.compareUnsigned(x, y) compares two int values as unsigned numbers. The bits are the same, but a number whose top bit is set counts as a very large value instead of a negative one.
That means -1 (all 32 bits set) is the biggest possible value, and Integer.MIN_VALUE is larger than Integer.MAX_VALUE. The result is negative, zero, or positive, just like Integer.compare.
A static method of java.lang.Integer (since Java 8): call it on the class, as Integer.compareUnsigned(...). Integer lives in java.lang, so no import is needed. compareUnsigned() was added in Java 8.
Syntax Pass the two numbers to compare.
int a = Integer.compare(-1, 1); // -1: -1 is less than 1
int b = Integer.compareUnsigned(-1, 1); // 1: 4294967295 is greater than 1 Call What it does Integer.compareUnsigned(x, y)Compares the bits of x and y as unsigned numbers. Integer.compare(x, y)The normal signed comparison. (a, b) -> Integer.compareUnsigned(a, b)A comparator that sorts unsigned values. Integer.toUnsignedLong(x)Widens to a long; comparing the longs gives the same answer.
Both arguments are always treated as unsigned. Use it only when your int values really stand for unsigned data, such as addresses, hashes, or bit masks.
Library declarations // java.lang.Integer
public static int compareUnsigned(int x, int y) Parameters Parameter Description x (int)The first number, read as unsigned. y (int)The second number, read as unsigned.
Return Value Return Value Description negative int x is less than y when both are unsigned.0The two values have the same bits. positive int x is greater than y when both are unsigned.
Important
This method was added in Java 8. It disagrees with Integer.compare whenever the two numbers have different signs, because a negative int counts as a large unsigned value. For values with the same sign, both methods agree. Do not use it for ordinary signed numbers.
Compare with Similar Methods Related methods:
Java Examples 1. Signed vs unsigned 2. Unsigned sorting 3. IP address range Example 1 — Compare the Same Pairs Both Ways Compare several pairs with compare and compareUnsigned, and see where they disagree.
public class IntegerCompareUnsignedDemo {
public static void main(String[] args) {
int[][] pairs = {{1, 2}, {-1, 1}, {1, -1}, {Integer.MIN_VALUE, Integer.MAX_VALUE}};
for (int[] pair : pairs) {
System.out.println(pair[0] + " vs " + pair[1]
+ ": signed " + Integer.compare(pair[0], pair[1])
+ ", unsigned " + Integer.compareUnsigned(pair[0], pair[1]));
}
}
} 1 vs 2: signed -1, unsigned -1
-1 vs 1: signed -1, unsigned 1
1 vs -1: signed 1, unsigned -1
-2147483648 vs 2147483647: signed -1, unsigned 1 How It Works Same signs agree. 1 and 2 are both small and positive, so both methods say the first is less.-1 is huge when unsigned. Its bits are all ones, which is 4294967295, so it is greater than 1 in the unsigned view.The order flips back. With the arguments swapped, the unsigned answer is -1 and the signed answer is 1.MIN_VALUE vs MAX_VALUE. Signed, MIN_VALUE is the smallest. Unsigned, it is 2147483648, which is bigger than 2147483647.Example 2 — Sort Values as Unsigned Numbers Sort the same values with a signed comparator and an unsigned one, and print each in unsigned form.
import java.util.Arrays;
public class IntegerCompareUnsignedSort {
static String show(Integer[] values) {
StringBuilder text = new StringBuilder();
for (Integer value : values) {
if (text.length() > 0) {
text.append(", ");
}
text.append(Integer.toUnsignedString(value));
}
return "[" + text + "]";
}
public static void main(String[] args) {
Integer[] values = {5, -1, 100, Integer.MIN_VALUE, Integer.MAX_VALUE};
Arrays.sort(values, (a, b) -> Integer.compare(a, b));
System.out.println("signed: " + show(values));
Arrays.sort(values, (a, b) -> Integer.compareUnsigned(a, b));
System.out.println("unsigned: " + show(values));
}
} signed: [2147483648, 4294967295, 5, 100, 2147483647]
unsigned: [5, 100, 2147483647, 2147483648, 4294967295] How It Works Same data, same print method. show always prints through toUnsignedString, so only the sort order differs.Signed order. The negative numbers come first: MIN_VALUE (2147483648 unsigned) and -1 (4294967295 unsigned), then 5, 100, and MAX_VALUE.Unsigned order. The numbers are now in true numeric order, so the two "negative" values move to the end.Boxed array. Arrays.sort with a comparator needs Integer[], not int[].Example 3 — Check an IP Address Range Pack addresses into int values and check a range with unsigned and signed comparisons.
public class IntegerCompareUnsignedRange {
static int ip(int a, int b, int c, int d) {
return (a << 24) | (b << 16) | (c << 8) | d;
}
public static void main(String[] args) {
int start = ip(100, 0, 0, 0);
int end = ip(220, 0, 0, 0);
int host = ip(150, 20, 30, 40);
boolean signed = host >= start && host <= end;
boolean unsigned = Integer.compareUnsigned(host, start) >= 0
&& Integer.compareUnsigned(host, end) <= 0;
System.out.println("start as int: " + start);
System.out.println("end as int: " + end);
System.out.println("host as int: " + host);
System.out.println("signed check: " + signed);
System.out.println("unsigned check: " + unsigned);
}
} start as int: 1677721600
end as int: -603979776
host as int: -1777066456
signed check: false
unsigned check: true How It Works Addresses are 32 bits. Each address is packed into an int by shifting its four numbers into place.Some look negative. Any address starting at 128 or above has its top bit set, so 220.0.0.0 and 150.20.30.40 are negative int values.The signed check fails. It sees a negative host that is smaller than the positive start of the range, so it answers false.The unsigned check is right. As unsigned numbers, 100.0.0.0 < 150.20.30.40 < 220.0.0.0, so the host is in the range.Frequently Asked Questions What does Integer.compareUnsigned do? It compares two ints as if they were unsigned 32-bit numbers (0 to 4294967295). It returns a negative number, zero, or a positive number.
Why is -1 greater than 1 with compareUnsigned? The bits of -1 are all ones, which is 4294967295 when read as unsigned. That is larger than 1.
When do compare and compareUnsigned give different answers? When the two numbers have different signs. If both are non-negative or both are negative, the answers match.
Which Java version added compareUnsigned? Java 8, along with the other unsigned methods in Integer and Long.
Does compareUnsigned change my numbers? No. It only changes how the bits are ordered for this one comparison. The ints keep their values.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion