In Java, Integer.reverse(i) returns the int you get by writing the 32 bits of i in the opposite order. The lowest bit becomes the highest, the second lowest becomes the second highest, and so on.
It is not the same as reversing the decimal digits of a number. Reversing the bits of 1 gives Integer.MIN_VALUE, because the single set bit moves from the far right to the far left.
A static method of java.lang.Integer (since Java 1.5): call it on the class, as Integer.reverse(...). Integer lives in java.lang, so no import is needed.
Syntax
One argument: the int whose bits you want to reverse.
Java
int a = Integer.reverse(1); // -2147483648 (the 1 moves to the far left)
int b = Integer.reverse(0); // 0
int c = Integer.reverse(Integer.reverse(123)); // 123
Call
What it does
Integer.reverse(i)
All 32 bits of i in reverse order.
Integer.reverse(i) >>> (32 - k)
Reverses only the lowest k bits of i.
Integer.reverseBytes(i)
Reverses the order of the four bytes, not the bits.
Integer.rotateLeft(i, d)
Moves bits around without reversing them.
Reversing twice returns the original number. Use >>> (unsigned shift) to bring the reversed bits down to a smaller width.
Library declarations
Java
// java.lang.Integer
public static int reverse(int i)
Parameters
Parameter
Description
i (int)
The number whose bits are reversed.
Return Value
Return Value
Description
int
The value whose bit n is bit 31 - n of i. It is often negative when the lowest bit of i was set.
Always 32 digits. The helper pads with zeros, so the mirror image is easy to see.
1 flips to the far left. The single set bit moves from the last position to the first, which makes the number Integer.MIN_VALUE.
11 is 1011. Reading the 4 low bits from the other side gives 1101 at the very left, so the result is negative.
6 is 110. It reverses to 011 at the far left, which is 1610612736.
Example 2 — Show That Reversing Twice Returns the Original
Reverse a few numbers twice and compare, then reverse only the low bits with an unsigned shift.
Java
public class IntegerReverseTwice {
public static void main(String[] args) {
int[] numbers = {123456, -77, 0, Integer.MAX_VALUE};
for (int n : numbers) {
int twice = Integer.reverse(Integer.reverse(n));
System.out.println(n + " -> back to " + twice + ", same: " + (n == twice));
}
int low4 = Integer.reverse(11) >>> 28;
System.out.println("low 4 bits of 11 reversed: " + Integer.toBinaryString(low4));
}
}
Output
123456 -> back to 123456, same: true
-77 -> back to -77, same: true
0 -> back to 0, same: true
2147483647 -> back to 2147483647, same: true
low 4 bits of 11 reversed: 1101
How It Works
A mirror of a mirror. Flipping the bit order twice restores every bit to its original place.
It holds for any number. Positive, negative, zero, and MAX_VALUE all come back unchanged.
Shrinking to 4 bits. Shifting the reversed value right by 28 keeps only the top 4 bits, which are the reversed low 4 bits.
1011 becomes 1101. The number 11 is 1011, and reading it backwards gives 1101, which is 13.
Example 3 — Create a Bit-Reversal Order for 8 Items
Reverse the 3-bit index of each of 8 positions to build the order that the fast Fourier transform and similar algorithms use.
Java
public class IntegerReverseOrder {
public static void main(String[] args) {
int bits = 3;
int size = 1 << bits;
for (int i = 0; i < size; i++) {
int reversed = Integer.reverse(i) >>> (32 - bits);
System.out.println(i + " (" + Integer.toBinaryString(i + 8).substring(1) + ") -> "
+ reversed + " (" + Integer.toBinaryString(reversed + 8).substring(1) + ")");
}
}
}