Home Java Integer Methods rotateLeft() Java Integer rotateLeft() Method 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.
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) Call What 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.lang.Integer
public static int rotateLeft(int i, int distance) Parameters Parameter Description 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 Value Description intThe rotated value. Its bit count is the same as that of i.
Important
The distance is taken modulo 32, so rotateLeft(i, 32) returns i unchanged and rotateLeft(i, -1) equals rotateRight(i, 1). A rotation never changes the number of 1 bits. The result may be negative if a 1 lands in the top position. Rotating is not the same as multiplying by a power of two: only a shift loses the left bits.
Compare with Similar Methods Related bit methods:
Java Examples 1. A circular shift 2. Distance rules 3. Mix bits for a hash Example 1 — Rotate Bits Left Rotate a few values and print them in hex, eight digits wide, so you can see bits wrap around.
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)));
}
} 1 by 3: 00000008
0x80000001 by 1: 00000003
0x80000001 << 1: 00000002
0x12345678 by 8: 34567812 How It Works A normal move. Rotating 1 left by 3 gives 8, the same as a shift, because nothing falls off the left end.The wrap-around. 0x80000001 has bits at both ends. Rotating by 1 sends the top bit around to the right, giving 00000003.A shift would lose it. 0x80000001 << 1 throws the top bit away and leaves 00000002.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.
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));
}
} by 0: 1
by 1: 2
by 32: 1
by 33: 2
by -1: -2147483648
right 1: -2147483648 How It Works Distance 0 and 32. Rotating by a full circle returns the original number, so both print 1.33 is 1. Only the low 5 bits of the distance count, so 33 behaves like 1 and gives 2.Negative means right. A distance of -1 is reduced to 31 places left, which equals one place right.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.
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}));
}
} 7, 13, 21: 7605
21, 13, 7: 21927
7, 13, 21 again: 7605 How It Works Rotate, then XOR. Each step moves the bits so far 5 places left and mixes in the next value with ^.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.Order matters. The same numbers in the opposite order give a different result, so the pattern is not just a sum.Deterministic. The same input always gives the same hash.Frequently Asked Questions What does Integer.rotateLeft do? 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.
What is the difference between rotateLeft and the << operator? The << operator drops the bits that leave on the left and fills with zeros. rotateLeft wraps them around, so no information is lost.
What happens if the distance is larger than 31? Only the low 5 bits of the distance are used. A distance of 32 does nothing, and 33 is the same as 1.
How do I rotate to the right? Use Integer.rotateRight(i, d), or pass a negative distance to rotateLeft.
Why would I rotate bits? Rotation is common in hash functions and checksums, where bits must be mixed without losing any of them.
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.
14 people found this page helpful
Helpful Share Copy link Suggestion