Java Integer rotateLeft() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

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

That is different from i << distance, which throws the left bits away and fills the right with zeros. A rotation is reversible and is often used when mixing bits for a hash value.

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

Syntax

Pass the number and how far to rotate.

Java
int a = Integer.rotateLeft(1, 3);              // 8
int b = Integer.rotateLeft(0x80000001, 1);     // 3 (the top bit wraps around)
int c = Integer.rotateLeft(1, 32);             // 1 (a full circle)
CallWhat it does
Integer.rotateLeft(i, d)Rotates i left by d places.
Integer.rotateRight(i, d)Rotates the other way.
i << dA normal shift: bits that leave on the left are lost.
Integer.rotateLeft(i, -d)A negative distance rotates to the right.

Only the low 5 bits of the distance count, so rotating by 32 does nothing, and rotating by 33 is the same as rotating by 1.

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

Parameters

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

Return Value

Return ValueDescription
intThe rotated value. Its bit count is the same as that of i.

Compare with Similar Methods

Related bit methods:

MethodHow it differs
rotateRight(i, d)Rotates in the opposite direction.
reverse(i)Reverses the bit order instead of rotating it.
bitCount(i)Stays the same after any rotation.
toBinaryString(i)Shows what a rotation did to the bits.

Java Examples

Example 1 — Rotate Bits Left

Rotate a few values and print them in hex, eight digits wide, so you can see bits wrap around.

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

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

How It Works

  1. A normal move. Rotating 1 left by 3 gives 8, the same as a shift, because nothing falls off the left end.
  2. The wrap-around. 0x80000001 has bits at both ends. Rotating by 1 sends the top bit around to the right, giving 00000003.
  3. A shift would lose it. 0x80000001 << 1 throws the top bit away and leaves 00000002.
  4. Whole bytes. Rotating by 8 moves the top byte 12 to the bottom: 34567812.

Example 2 — See How the Distance Is Reduced

Rotate by 0, 32, 33, and a negative distance, and compare with rotateRight.

Java
public class IntegerRotateLeftDistance {
    public static void main(String[] args) {
        int value = 1;

        System.out.println("by 0:   " + Integer.rotateLeft(value, 0));
        System.out.println("by 1:   " + Integer.rotateLeft(value, 1));
        System.out.println("by 32:  " + Integer.rotateLeft(value, 32));
        System.out.println("by 33:  " + Integer.rotateLeft(value, 33));
        System.out.println("by -1:  " + Integer.rotateLeft(value, -1));
        System.out.println("right 1: " + Integer.rotateRight(value, 1));
    }
}

How It Works

  1. Distance 0 and 32. Rotating by a full circle returns the original number, so both print 1.
  2. 33 is 1. Only the low 5 bits of the distance count, so 33 behaves like 1 and gives 2.
  3. Negative means right. A distance of -1 is reduced to 31 places left, which equals one place right.
  4. Same as rotateRight. The last two lines match: the single set bit wraps to the top and gives MIN_VALUE.

Example 3 — Mix Numbers into a Simple Hash

Combine several numbers into one value by rotating the running result before XORing the next number into it.

Java
public class IntegerRotateLeftHash {
    static int mix(int[] values) {
        int hash = 0;
        for (int value : values) {
            hash = Integer.rotateLeft(hash, 5) ^ value;
        }
        return hash;
    }

    public static void main(String[] args) {
        System.out.println("7, 13, 21:  " + mix(new int[] {7, 13, 21}));
        System.out.println("21, 13, 7:  " + mix(new int[] {21, 13, 7}));
        System.out.println("7, 13, 21 again: " + mix(new int[] {7, 13, 21}));
    }
}

How It Works

  1. Rotate, then XOR. Each step moves the bits so far 5 places left and mixes in the next value with ^.
  2. Following the first call. The hash goes 0, then 7, then (7 rotated is 224) 224 XOR 13 is 237, then (237 rotated is 7584) 7584 XOR 21 is 7605.
  3. Order matters. The same numbers in the opposite order give a different result, so the pattern is not just a sum.
  4. Deterministic. The same input always gives the same hash.

Frequently Asked Questions

It moves the bits of an int to the left by a given distance, and bits that leave on the left come back in on the right.
The << operator drops the bits that leave on the left and fills with zeros. rotateLeft wraps them around, so no information is lost.
Only the low 5 bits of the distance are used. A distance of 32 does nothing, and 33 is the same as 1.
Use Integer.rotateRight(i, d), or pass a negative distance to rotateLeft.
Rotation is common in hash functions and checksums, where bits must be mixed without losing any of them.

Next: rotateRight()

Rotate the bits of an int to the right in a circle.

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