Java Integer rotateRight() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.rotateRight(i, distance) moves every bit of i to the right by distance places. Bits that fall off the right end come back in on the left, so no bit is lost.

That is different from i >>> distance, which throws the right bits away and fills the left with zeros. Rotating right by d undoes rotating left by d.

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

Syntax

Pass the number and how far to rotate.

Java
int a = Integer.rotateRight(8, 3);         // 1
int b = Integer.rotateRight(1, 1);         // -2147483648 (the bit wraps to the top)
int c = Integer.rotateRight(1, 32);        // 1 (a full circle)
CallWhat it does
Integer.rotateRight(i, d)Rotates i right by d places.
Integer.rotateLeft(i, d)Rotates the other way.
i >>> dA normal unsigned shift: bits that leave on the right are lost.
Integer.rotateRight(i, 16)Swaps the high and low 16-bit halves.

Only the low 5 bits of the distance count, so a distance of 32 does nothing and a negative distance rotates left.

Library declarations
Java
// java.lang.Integer
public static int rotateRight(int i, int distance)

Parameters

ParameterDescription
i (int)The number whose bits are rotated.
distance (int)How many places to rotate right. It is reduced to the range 0 to 31, so a negative value rotates left.

Return Value

Return ValueDescription
intThe rotated value. Its bit count equals that of i.

Compare with Similar Methods

Related bit methods:

MethodHow it differs
rotateLeft(i, d)Rotates in the opposite direction.
reverseBytes(i)Reverses the four bytes instead of rotating them.
highestOneBit(i)Finds the top bit that a rotation might move.
toHexString(i)Shows the effect of a rotation clearly.

Java Examples

Example 1 — Rotate Bits Right

Rotate a few values right and compare with an unsigned shift, in hex.

Java
public class IntegerRotateRightDemo {
    static String hex(int n) {
        return String.format("%08x", n);
    }

    public static void main(String[] args) {
        System.out.println("8 by 3:           " + hex(Integer.rotateRight(8, 3)));
        System.out.println("1 by 1:           " + hex(Integer.rotateRight(1, 1)));
        System.out.println("1 >>> 1:          " + hex(1 >>> 1));
        System.out.println("0x12345678 by 8:  " + hex(Integer.rotateRight(0x12345678, 8)));
    }
}

How It Works

  1. A normal move. Rotating 8 right by 3 gives 1, the same as a shift, because no set bit falls off.
  2. Wrapping around. The single bit of 1 leaves on the right and comes back on the left, giving 80000000.
  3. A shift loses it. 1 >>> 1 throws the bit away and leaves 00000000.
  4. Whole bytes. Rotating right by 8 moves the low byte 78 to the top: 78123456.

Example 2 — Undo a Rotation with the Opposite Method

Rotate left, then rotate right by the same distance, and check that every value comes back.

Java
public class IntegerRotateRightInverse {
    public static void main(String[] args) {
        int value = 0x12345678;
        int[] distances = {4, 12, 31, 40};

        for (int d : distances) {
            int rotated = Integer.rotateLeft(value, d);
            int back = Integer.rotateRight(rotated, d);
            System.out.println("distance " + d + ": " + String.format("%08x", rotated)
                    + " -> " + String.format("%08x", back) + ", same: " + (back == value));
        }
    }
}

How It Works

  1. Opposite directions cancel. Moving the bits left by d and then right by d puts every bit back.
  2. Nibbles move. Rotating by 4 shifts each hex digit one place left, and rotating by 12 shifts three places.
  3. Large distances. 31 is one place right in disguise, and 40 is reduced to 8, so both still undo cleanly.
  4. Always the same. The final column is true in every row.

Example 3 — Swap the Two 16-Bit Halves of an int

Rotate by 16 to swap the upper and lower halves of a number, and show that doing it twice restores the number.

Java
public class IntegerRotateRightHalves {
    static String hex(int n) {
        return String.format("%08x", n);
    }

    public static void main(String[] args) {
        int value = 0xAABBCCDD;
        int swapped = Integer.rotateRight(value, 16);

        System.out.println("original: " + hex(value));
        System.out.println("swapped:  " + hex(swapped));
        System.out.println("again:    " + hex(Integer.rotateRight(swapped, 16)));
        System.out.println("high half: " + Integer.toHexString(value >>> 16));
        System.out.println("low half:  " + Integer.toHexString(value & 0xFFFF));
    }
}

How It Works

  1. Half a circle. A rotation by 16 places is exactly half of 32, so the two 16-bit halves trade places.
  2. aabb and ccdd. The original is aabbccdd, with aabb on top, and the swapped value is ccddaabb.
  3. Twice is a no-op. Rotating by 16 again puts the halves back.
  4. Reading the halves. >>> 16 gives the upper half and & 0xFFFF gives the lower half.

Frequently Asked Questions

It moves the bits of an int to the right by a given distance, and bits that leave on the right come back in on the left.
The >>> operator discards the bits that leave on the right and fills with zeros. rotateRight wraps them around.
Rotate left by the same distance with Integer.rotateLeft.
Only the low 5 bits of the distance are used, so 32 changes nothing and 33 acts like 1.
Call Integer.rotateRight(value, 16). Because 16 is half of 32, the high and low halves trade places.

Next: shortValue()

Narrow an Integer to a primitive short.

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

15 people found this page helpful