Java Integer compareUnsigned() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

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.

Java
int a = Integer.compare(-1, 1);          // -1: -1 is less than 1
int b = Integer.compareUnsigned(-1, 1);  // 1: 4294967295 is greater than 1
CallWhat 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
// java.lang.Integer
public static int compareUnsigned(int x, int y)

Parameters

ParameterDescription
x (int)The first number, read as unsigned.
y (int)The second number, read as unsigned.

Return Value

Return ValueDescription
negative intx is less than y when both are unsigned.
0The two values have the same bits.
positive intx is greater than y when both are unsigned.

Compare with Similar Methods

Related methods:

MethodHow it differs
compare(x, y)Signed comparison: negative numbers are smaller than positive ones.
divideUnsigned(x, y)Unsigned division.
toUnsignedLong(x)Widens an int to a non-negative long.
toUnsignedString(x)Prints a value as an unsigned number.

Java Examples

Example 1 — Compare the Same Pairs Both Ways

Compare several pairs with compare and compareUnsigned, and see where they disagree.

Java
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]));
        }
    }
}

How It Works

  1. Same signs agree. 1 and 2 are both small and positive, so both methods say the first is less.
  2. -1 is huge when unsigned. Its bits are all ones, which is 4294967295, so it is greater than 1 in the unsigned view.
  3. The order flips back. With the arguments swapped, the unsigned answer is -1 and the signed answer is 1.
  4. 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.

Java
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));
    }
}

How It Works

  1. Same data, same print method. show always prints through toUnsignedString, so only the sort order differs.
  2. Signed order. The negative numbers come first: MIN_VALUE (2147483648 unsigned) and -1 (4294967295 unsigned), then 5, 100, and MAX_VALUE.
  3. Unsigned order. The numbers are now in true numeric order, so the two "negative" values move to the end.
  4. 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.

Java
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);
    }
}

How It Works

  1. Addresses are 32 bits. Each address is packed into an int by shifting its four numbers into place.
  2. 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.
  3. The signed check fails. It sees a negative host that is smaller than the positive start of the range, so it answers false.
  4. 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

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.
The bits of -1 are all ones, which is 4294967295 when read as unsigned. That is larger than 1.
When the two numbers have different signs. If both are non-negative or both are negative, the answers match.
Java 8, along with the other unsigned methods in Integer and Long.
No. It only changes how the bits are ordered for this one comparison. The ints keep their values.

Next: decode()

Parse text that may use a 0x, #, or 0 prefix.

decode() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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