Home Java Integer Methods remainderUnsigned() Java Integer remainderUnsigned() Method Definition In Java, Integer.remainderUnsigned(dividend, divisor) returns what is left over when two int values are divided as unsigned numbers. The result is never negative.
The % operator is signed and can return a negative remainder. This method is the right choice for hash values and counters whose bit pattern should be read as a large positive number.
A static method of java.lang.Integer (since Java 8): call it on the class, as Integer.remainderUnsigned(...). Integer lives in java.lang, so no import is needed. remainderUnsigned() was added in Java 8.
Syntax Pass the dividend first, then the divisor.
int a = Integer.remainderUnsigned(100, 7); // 2
int b = Integer.remainderUnsigned(-1, 10); // 5 (4294967295 % 10)
int c = -1 % 10; // -1 (signed remainder) Call What it does Integer.remainderUnsigned(a, b)Unsigned remainder of a divided by b. Integer.divideUnsigned(a, b)The matching quotient. a % bNormal signed remainder; its sign follows a. Math.floorMod(a, b)A signed alternative that never returns a negative number for a positive b.
Both arguments are read as unsigned. For divisors that fit in an int the result also fits, so it is always safe to print directly.
Library declarations // java.lang.Integer
public static int remainderUnsigned(int dividend, int divisor) Parameters Parameter Description dividend (int)The number to divide, read as unsigned. divisor (int)The number to divide by, read as unsigned. It must not be 0.
Return Value Return Value Description intThe unsigned remainder, from 0 up to one less than the divisor.
Important
This method was added in Java 8. A divisor of 0 throws ArithmeticException. Together with divideUnsigned it satisfies quotient * divisor + remainder == dividend in int arithmetic. If a hash code is negative, hash % size can be negative and break an array index, while remainderUnsigned(hash, size) never is.
Compare with Similar Methods Related methods:
Java Examples 1. Signed vs unsigned 2. Hash bucket index 3. Quotient and remainder Example 1 — Compare % and remainderUnsigned Get the remainder of the same pairs with the % operator and with remainderUnsigned.
public class IntegerRemainderUnsignedDemo {
public static void main(String[] args) {
int[][] pairs = {{100, 7}, {-1, 10}, {-1, 7}, {Integer.MIN_VALUE, 3}};
for (int[] pair : pairs) {
System.out.println(pair[0] + " rem " + pair[1]
+ ": signed " + (pair[0] % pair[1])
+ ", unsigned " + Integer.remainderUnsigned(pair[0], pair[1]));
}
}
} 100 rem 7: signed 2, unsigned 2
-1 rem 10: signed -1, unsigned 5
-1 rem 7: signed -1, unsigned 3
-2147483648 rem 3: signed -2, unsigned 2 How It Works Positive numbers agree. 100 divided by 7 leaves 2 with both.A negative dividend. The signed remainder of -1 is -1, but as the unsigned number 4294967295 divided by 10, the remainder is 5.Another divisor. 4294967295 is 7 × 613566756 plus 3, so the unsigned remainder is 3.MIN_VALUE. It reads as 2147483648, which leaves 2 after division by 3, while the signed remainder is -2.Example 2 — Pick a Bucket Index for Hash Values Map hash codes, some of them negative, to bucket numbers from 0 to 9.
public class IntegerRemainderUnsignedBucket {
public static void main(String[] args) {
int buckets = 10;
int[] hashes = {12345, -7, -1234567, Integer.MIN_VALUE};
for (int hash : hashes) {
System.out.println(hash + ": % gives " + (hash % buckets)
+ ", remainderUnsigned gives " + Integer.remainderUnsigned(hash, buckets));
}
}
} 12345: % gives 5, remainderUnsigned gives 5
-7: % gives -7, remainderUnsigned gives 9
-1234567: % gives -7, remainderUnsigned gives 9
-2147483648: % gives -8, remainderUnsigned gives 8 How It Works Hash codes can be negative. Many hash codes, such as those of long strings, come out below zero.% breaks the index. For -7, -1234567, and MIN_VALUE the % operator returns a negative number, which cannot be an array index.remainderUnsigned is always valid. It reads every hash as an unsigned number, so the result is always between 0 and 9.Positive hashes agree. 12345 leaves 5 with both methods.Example 3 — Check That Quotient and Remainder Rebuild the Number Split a value with divideUnsigned and remainderUnsigned, then rebuild it.
public class IntegerRemainderUnsignedIdentity {
public static void main(String[] args) {
int number = -1;
int divisor = 7;
int quotient = Integer.divideUnsigned(number, divisor);
int remainder = Integer.remainderUnsigned(number, divisor);
System.out.println("number: " + Integer.toUnsignedString(number));
System.out.println("quotient: " + quotient);
System.out.println("remainder: " + remainder);
System.out.println("rebuilt: " + (quotient * divisor + remainder));
System.out.println("matches: " + (quotient * divisor + remainder == number));
try {
Integer.remainderUnsigned(number, 0);
} catch (ArithmeticException e) {
System.out.println("zero divisor -> ArithmeticException");
}
}
} number: 4294967295
quotient: 613566756
remainder: 3
rebuilt: -1
matches: true
zero divisor -> ArithmeticException How It Works The unsigned view. The variable -1 stands for 4294967295 in this example.Two results. 4294967295 divided by 7 gives a quotient of 613566756 and a remainder of 3.Rebuilding. 613566756 × 7 + 3 is 4294967295, which an int keeps as -1, the original bits.Zero is rejected. A zero divisor throws ArithmeticException, as it does for every division method.Frequently Asked Questions What does Integer.remainderUnsigned do? It returns the remainder of dividing two ints as unsigned 32-bit numbers. The result is never negative.
Why is -1 % 10 negative but remainderUnsigned(-1, 10) is 5? The % operator keeps the sign of the dividend. remainderUnsigned reads -1 as 4294967295, whose remainder after dividing by 10 is 5.
How do I turn a negative hash code into a safe bucket index? Use Integer.remainderUnsigned(hash, size), or Math.floorMod(hash, size). Both always return a value from 0 to size - 1.
Does remainderUnsigned throw on a zero divisor? Yes. It throws ArithmeticException, just like the % operator.
What is the relation between divideUnsigned and remainderUnsigned? quotient * divisor + remainder equals the original number, when the arithmetic is done in int.
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