Java Integer remainderUnsigned() Method

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

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.

Java
int a = Integer.remainderUnsigned(100, 7);  // 2
int b = Integer.remainderUnsigned(-1, 10);   // 5 (4294967295 % 10)
int c = -1 % 10;                             // -1 (signed remainder)
CallWhat 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
// java.lang.Integer
public static int remainderUnsigned(int dividend, int divisor)

Parameters

ParameterDescription
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 ValueDescription
intThe unsigned remainder, from 0 up to one less than the divisor.

Compare with Similar Methods

Related methods:

MethodHow it differs
divideUnsigned(a, b)The unsigned quotient of the same division.
compareUnsigned(a, b)Unsigned comparison.
hashCode()Where negative hash values come from.
toUnsignedString(i)Prints a value as an unsigned number.

Java Examples

Example 1 — Compare % and remainderUnsigned

Get the remainder of the same pairs with the % operator and with remainderUnsigned.

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

How It Works

  1. Positive numbers agree. 100 divided by 7 leaves 2 with both.
  2. A negative dividend. The signed remainder of -1 is -1, but as the unsigned number 4294967295 divided by 10, the remainder is 5.
  3. Another divisor. 4294967295 is 7 × 613566756 plus 3, so the unsigned remainder is 3.
  4. 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.

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

How It Works

  1. Hash codes can be negative. Many hash codes, such as those of long strings, come out below zero.
  2. % breaks the index. For -7, -1234567, and MIN_VALUE the % operator returns a negative number, which cannot be an array index.
  3. remainderUnsigned is always valid. It reads every hash as an unsigned number, so the result is always between 0 and 9.
  4. 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.

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

How It Works

  1. The unsigned view. The variable -1 stands for 4294967295 in this example.
  2. Two results. 4294967295 divided by 7 gives a quotient of 613566756 and a remainder of 3.
  3. Rebuilding. 613566756 × 7 + 3 is 4294967295, which an int keeps as -1, the original bits.
  4. Zero is rejected. A zero divisor throws ArithmeticException, as it does for every division method.

Frequently Asked Questions

It returns the remainder of dividing two ints as unsigned 32-bit numbers. The result is never negative.
The % operator keeps the sign of the dividend. remainderUnsigned reads -1 as 4294967295, whose remainder after dividing by 10 is 5.
Use Integer.remainderUnsigned(hash, size), or Math.floorMod(hash, size). Both always return a value from 0 to size - 1.
Yes. It throws ArithmeticException, just like the % operator.
quotient * divisor + remainder equals the original number, when the arithmetic is done in int.

Next: reverse()

Reverse the order of all 32 bits of an int.

reverse() 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