Java Integer reverse() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

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
CallWhat 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

ParameterDescription
i (int)The number whose bits are reversed.

Return Value

Return ValueDescription
intThe value whose bit n is bit 31 - n of i. It is often negative when the lowest bit of i was set.

Compare with Similar Methods

Related bit methods:

MethodHow it differs
reverseBytes(i)Reverses bytes instead of single bits.
rotateLeft(i, d)Rotates all bits left; the bit order stays the same.
toBinaryString(i)Shows both the original and the reversed pattern.
numberOfLeadingZeros(i)The leading zeros of the original become the trailing zeros after a reverse.

Java Examples

Example 1 — Reverse the Bits of Small Numbers

Reverse a few numbers and print the 32-bit results in binary, so you can see the mirror image.

Java
public class IntegerReverseDemo {
    static String bits(int n) {
        return String.format("%32s", Integer.toBinaryString(n)).replace(' ', '0');
    }

    public static void main(String[] args) {
        int[] numbers = {1, 11, 6};

        for (int n : numbers) {
            int r = Integer.reverse(n);
            System.out.println(bits(n));
            System.out.println(bits(r) + "  (" + r + ")");
        }
    }
}

How It Works

  1. Always 32 digits. The helper pads with zeros, so the mirror image is easy to see.
  2. 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.
  3. 11 is 1011. Reading the 4 low bits from the other side gives 1101 at the very left, so the result is negative.
  4. 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));
    }
}

How It Works

  1. A mirror of a mirror. Flipping the bit order twice restores every bit to its original place.
  2. It holds for any number. Positive, negative, zero, and MAX_VALUE all come back unchanged.
  3. Shrinking to 4 bits. Shifting the reversed value right by 28 keeps only the top 4 bits, which are the reversed low 4 bits.
  4. 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) + ")");
        }
    }
}

How It Works

  1. Eight positions use 3 bits. The indexes 0 to 7 each fit in 3 binary digits.
  2. Reverse, then shrink. Integer.reverse(i) works on 32 bits, so shifting right by 32 - 3 keeps just the reversed low 3 bits.
  3. Padding trick. Adding 8 and cutting off the first character writes each number with exactly three digits.
  4. The new order. The indexes come out as 0, 4, 2, 6, 1, 5, 3, 7, the order used by bit-reversal permutations.

Frequently Asked Questions

It reverses the order of all 32 bits of an int. The lowest bit becomes the highest and vice versa.
No. It reverses bits, not decimal digits. Reversing 123 as digits gives 321; Integer.reverse(123) gives a completely different, usually huge number.
The only set bit moves from the far right to the far left, which is the sign bit, so the result is -2147483648.
Use Integer.reverse(i) >>> (32 - k). The unsigned shift moves the reversed bits down into the low positions.
reverse flips all 32 bits individually. reverseBytes only swaps the four bytes and leaves the bits inside each byte as they are.

Next: reverseBytes()

Reverse the order of the four bytes of an int.

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

12 people found this page helpful