Java Integer reverseBytes() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.reverseBytes(i) returns the int you get by writing the four bytes of i in the opposite order. The first byte becomes the last, and the second becomes the third.

This converts between big-endian and little-endian byte order, which different systems and file formats use. The bits inside each byte keep their order.

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

Syntax

One argument: the int whose bytes you want to swap.

Java
int a = Integer.reverseBytes(0x01020304);    // 0x04030201
int b = Integer.reverseBytes(1);             // 16777216 (0x01000000)
int c = Integer.reverseBytes(Integer.reverseBytes(42));  // 42
CallWhat it does
Integer.reverseBytes(i)Swaps the order of the four bytes of i.
String.format("%08x", i)Shows an int as eight hex digits, so the bytes are easy to read.
Integer.reverse(i)Reverses every bit, not just whole bytes.
Integer.rotateLeft(i, 8)Moves bytes in a circle, which is a different reordering.

Each pair of hex digits is one byte, so reverseBytes swaps pairs of hex digits, never single digits.

Library declarations
Java
// java.lang.Integer
public static int reverseBytes(int i)

Parameters

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

Return Value

Return ValueDescription
intThe number with its bytes in reverse order. The bits within each byte are unchanged.

Compare with Similar Methods

Related bit methods:

MethodHow it differs
reverse(i)Reverses every bit instead of whole bytes.
rotateLeft(i, d)Moves bits in a circle without reversing them.
toHexString(i)Shows the bytes in hex to see what changed.
byteValue()Extracts a single byte of a number.

Java Examples

Example 1 — Swap the Byte Order of an int

Reverse the bytes of a recognizable pattern and print both in hex, eight digits wide.

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

    public static void main(String[] args) {
        int original = 0x01020304;
        int swapped = Integer.reverseBytes(original);

        System.out.println("original: " + hex(original));
        System.out.println("swapped:  " + hex(swapped));
        System.out.println("swapped as int: " + swapped);
        System.out.println("twice:    " + hex(Integer.reverseBytes(swapped)));
    }
}

How It Works

  1. Four bytes, four pairs. The hex digits 01, 02, 03, and 04 are the four bytes of the number.
  2. Bytes swap places. The order becomes 04, 03, 02, 01. The digits inside each pair stay together.
  3. A different number. The swapped pattern 04030201 is 67305985 as a decimal int.
  4. Swapping twice is a no-op. A second call restores 01020304.

Example 2 — See How Small Numbers Turn into Big Ones

Reverse the bytes of several small and negative numbers and see their hex and decimal forms.

Java
public class IntegerReverseBytesValues {
    public static void main(String[] args) {
        int[] numbers = {1, 256, 65536, 255, -1};

        for (int n : numbers) {
            int swapped = Integer.reverseBytes(n);
            System.out.println(n + " (" + String.format("%08x", n) + ") -> "
                    + swapped + " (" + String.format("%08x", swapped) + ")");
        }
    }
}

How It Works

  1. The 1 moves to the top byte. 1 is 00000001, and after swapping it is 01000000, which is 16777216.
  2. 256 and 65536. They are 00000100 and 00010000, and they swap to 65536 and 256.
  3. 255 becomes negative. The byte ff moves to the top, so the sign bit is set and the number is -16777216.
  4. All ones are unchanged. -1 is eight f digits, and swapping identical bytes changes nothing.

Example 3 — Reverse the Bytes of an IPv4 Address

Pack an IPv4 address into an int, swap its bytes, and print both as dotted text.

Java
public class IntegerReverseBytesIp {
    static String dotted(int ip) {
        return ((ip >>> 24) & 0xFF) + "." + ((ip >>> 16) & 0xFF) + "."
                + ((ip >>> 8) & 0xFF) + "." + (ip & 0xFF);
    }

    public static void main(String[] args) {
        int ip = (192 << 24) | (168 << 16) | (1 << 8) | 10;

        System.out.println("address:  " + dotted(ip));
        System.out.println("reversed: " + dotted(Integer.reverseBytes(ip)));
        System.out.println("back:     " + dotted(Integer.reverseBytes(Integer.reverseBytes(ip))));
    }
}

How It Works

  1. One byte per part. The four parts of an address are exactly the four bytes of the packed int.
  2. The helper reads each byte. >>> and & 0xFF pull out one byte at a time, from the top.
  3. Reversed address. Swapping the bytes turns 192.168.1.10 into 10.1.168.192, the same four parts in the opposite order.
  4. Undo. Reversing again returns 192.168.1.10.

Frequently Asked Questions

It reverses the order of the four bytes of an int. 0x01020304 becomes 0x04030201.
reverse flips all 32 bits one by one. reverseBytes swaps whole bytes and keeps the bits inside each byte in order.
When you convert between big-endian and little-endian byte order, for example when reading binary files or network data from another system.
Yes. Reversing the byte order twice restores the number.
The set bits move into the top byte. For example, 1 becomes 16777216, and 255 becomes -16777216.

Next: rotateLeft()

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

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

13 people found this page helpful