Java Integer toUnsignedLong() Method

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

Definition

In Java, Integer.toUnsignedLong(x) returns a long that holds the 32 bits of x read as an unsigned number. The result is always between 0 and 4294967295.

A normal cast like (long) x copies the sign, so -1 stays -1. toUnsignedLong(-1) gives 4294967295 instead, which is what you want for values such as addresses and checksums stored in an int.

A static method of java.lang.Integer (since Java 8): call it on the class, as Integer.toUnsignedLong(...). Integer lives in java.lang, so no import is needed. toUnsignedLong() was added in Java 8.

Syntax

One argument: the int to widen.

Java
long a = Integer.toUnsignedLong(100);   // 100
long b = Integer.toUnsignedLong(-1);    // 4294967295
long c = (long) -1;                     // -1 (sign is kept)
CallWhat it does
Integer.toUnsignedLong(x)The unsigned value of x as a long.
(long) xNormal widening: a negative int stays negative.
x & 0xFFFFFFFFLThe classic way to do the same thing; it works on every Java version.
Integer.toUnsignedString(x)The same value as text.

The method does not change x. It only produces a long copy, so later calculations cannot overflow an int.

Library declarations
Java
// java.lang.Integer
public static long toUnsignedLong(int x)

Parameters

ParameterDescription
x (int)The 32 bits to read as an unsigned number.

Return Value

Return ValueDescription
longThe unsigned value of x, from 0 to 4294967295.

Compare with Similar Methods

Related methods:

MethodHow it differs
toUnsignedString(x)The same unsigned value as text.
parseUnsignedInt(s)Reads unsigned text into an int.
compareUnsigned(a, b)Compares two unsigned values without widening.
longValue()Normal signed widening of an Integer.

Java Examples

Example 1 — Widen Negative Numbers Two Ways

Widen the same values with a cast and with toUnsignedLong.

Java
public class IntegerToUnsignedLongDemo {
    public static void main(String[] args) {
        int[] numbers = {100, Integer.MAX_VALUE, Integer.MIN_VALUE, -2, -1};
        for (int n : numbers) {
            System.out.println(n + ": cast " + (long) n
                    + ", unsigned " + Integer.toUnsignedLong(n));
        }
    }
}

How It Works

  1. Non-negative values match. 100 and MAX_VALUE are the same with both approaches.
  2. A cast keeps the sign. The cast of -1 is -1, because widening copies the top bit into the new bits.
  3. toUnsignedLong adds 4294967296. A negative value becomes x + 4294967296, so -1 turns into 4294967295 and -2 into 4294967294.
  4. MIN_VALUE. It becomes 2147483648, which does not fit in an int but fits easily in a long.

Example 2 — Add Two Large Unsigned Values Safely

Add two values above Integer.MAX_VALUE in an int and in a long, and see the difference.

Java
public class IntegerToUnsignedLongSum {
    public static void main(String[] args) {
        int a = Integer.parseUnsignedInt("3000000000");
        int b = Integer.parseUnsignedInt("3000000000");

        int wrong = a + b;
        long right = Integer.toUnsignedLong(a) + Integer.toUnsignedLong(b);

        System.out.println("a as int:      " + a);
        System.out.println("int sum:       " + wrong);
        System.out.println("int unsigned:  " + Integer.toUnsignedString(wrong));
        System.out.println("long sum:      " + right);
    }
}

How It Works

  1. Values in an int. 3000000000 does not fit a signed int, so a holds the bits and prints as -1294967296.
  2. The int sum overflows. The true total is 6000000000, but an int keeps only 32 bits, which leaves 1705032704.
  3. Widen first. toUnsignedLong turns each operand into a long with the right positive value, so the addition cannot overflow.
  4. The right answer. The long sum is 6000000000.

Example 3 — Compare toUnsignedLong with the Mask

Show that toUnsignedLong and x & 0xFFFFFFFFL give the same answer, and that a plain long constant does not.

Java
public class IntegerToUnsignedLongMask {
    public static void main(String[] args) {
        int x = -1;

        System.out.println("toUnsignedLong:  " + Integer.toUnsignedLong(x));
        System.out.println("x & 0xFFFFFFFFL: " + (x & 0xFFFFFFFFL));
        System.out.println("same:            " + (Integer.toUnsignedLong(x) == (x & 0xFFFFFFFFL)));
        System.out.println("x & 0xFFFFFFFF:  " + (x & 0xFFFFFFFF));
        System.out.println("hex of long:     " + Long.toHexString(Integer.toUnsignedLong(x)));
    }
}

How It Works

  1. Two ways, one answer. Both the method and the mask keep exactly the low 32 bits, so they print 4294967295.
  2. The L matters. 0xFFFFFFFFL is a long constant. Without the L, 0xFFFFFFFF is an int equal to -1, so the AND returns -1.
  3. The hex view. As a long, the value is just eight f digits: ffffffff, with no sign bits above them.
  4. Prefer the method. It states your intention more clearly than a magic mask.

Frequently Asked Questions

It converts an int to a long and treats the int bits as an unsigned number. Integer.toUnsignedLong(-1) returns 4294967295.
A cast keeps the sign, so -1 stays -1. toUnsignedLong gives a non-negative long: for negative x it returns x + 4294967296.
Yes. toUnsignedLong is a clearer way to write it. Remember the L: without it the mask is an int and does not widen.
For checksums, IP addresses, file formats, and hashes that use 32 unsigned bits, while Java has only signed ints.
Java 8. On older versions use x & 0xFFFFFFFFL.

Next: toUnsignedString()

Print an int as an unsigned decimal number.

toUnsignedString() 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.

14 people found this page helpful