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)
Call
What it does
Integer.toUnsignedLong(x)
The unsigned value of x as a long.
(long) x
Normal widening: a negative int stays negative.
x & 0xFFFFFFFFL
The 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)
Non-negative values match. 100 and MAX_VALUE are the same with both approaches.
A cast keeps the sign. The cast of -1 is -1, because widening copies the top bit into the new bits.
toUnsignedLong adds 4294967296. A negative value becomes x + 4294967296, so -1 turns into 4294967295 and -2 into 4294967294.
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);
}
}
Output
a as int: -1294967296
int sum: 1705032704
int unsigned: 1705032704
long sum: 6000000000
How It Works
Values in an int. 3000000000 does not fit a signed int, so a holds the bits and prints as -1294967296.
The int sum overflows. The true total is 6000000000, but an int keeps only 32 bits, which leaves 1705032704.
Widen first.toUnsignedLong turns each operand into a long with the right positive value, so the addition cannot overflow.
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)));
}
}
Output
toUnsignedLong: 4294967295
x & 0xFFFFFFFFL: 4294967295
same: true
x & 0xFFFFFFFF: -1
hex of long: ffffffff
How It Works
Two ways, one answer. Both the method and the mask keep exactly the low 32 bits, so they print 4294967295.
The L matters.0xFFFFFFFFL is a long constant. Without the L, 0xFFFFFFFF is an int equal to -1, so the AND returns -1.
The hex view. As a long, the value is just eight f digits: ffffffff, with no sign bits above them.
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.