Home Java Integer Methods reverseBytes() Java Integer reverseBytes() Method 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.
int a = Integer.reverseBytes(0x01020304); // 0x04030201
int b = Integer.reverseBytes(1); // 16777216 (0x01000000)
int c = Integer.reverseBytes(Integer.reverseBytes(42)); // 42 Call What 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.lang.Integer
public static int reverseBytes(int i) Parameters Parameter Description i (int)The number whose bytes are reversed.
Return Value Return Value Description intThe number with its bytes in reverse order. The bits within each byte are unchanged.
Important
The result is a bit pattern. Small numbers become huge: reverseBytes(1) is 16777216, and a number with a set top byte becomes negative. Calling the method twice returns the original number. Java itself stores numbers in big-endian order in a ByteBuffer by default, so you rarely need this unless you read data written by a little-endian system.
Compare with Similar Methods Related bit methods:
Java Examples 1. Swap the bytes 2. Small numbers get big 3. IPv4 byte order Example 1 — Swap the Byte Order of an int Reverse the bytes of a recognizable pattern and print both in hex, eight digits wide.
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)));
}
} original: 01020304
swapped: 04030201
swapped as int: 67305985
twice: 01020304 How It Works Four bytes, four pairs. The hex digits 01, 02, 03, and 04 are the four bytes of the number.Bytes swap places. The order becomes 04, 03, 02, 01. The digits inside each pair stay together.A different number. The swapped pattern 04030201 is 67305985 as a decimal int.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.
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) + ")");
}
}
} 1 (00000001) -> 16777216 (01000000)
256 (00000100) -> 65536 (00010000)
65536 (00010000) -> 256 (00000100)
255 (000000ff) -> -16777216 (ff000000)
-1 (ffffffff) -> -1 (ffffffff) How It Works The 1 moves to the top byte. 1 is 00000001, and after swapping it is 01000000, which is 16777216.256 and 65536. They are 00000100 and 00010000, and they swap to 65536 and 256.255 becomes negative. The byte ff moves to the top, so the sign bit is set and the number is -16777216.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.
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))));
}
} address: 192.168.1.10
reversed: 10.1.168.192
back: 192.168.1.10 How It Works One byte per part. The four parts of an address are exactly the four bytes of the packed int.The helper reads each byte. >>> and & 0xFF pull out one byte at a time, from the top.Reversed address. Swapping the bytes turns 192.168.1.10 into 10.1.168.192, the same four parts in the opposite order.Undo. Reversing again returns 192.168.1.10.Frequently Asked Questions What does Integer.reverseBytes do? It reverses the order of the four bytes of an int. 0x01020304 becomes 0x04030201.
What is the difference between reverse and reverseBytes? reverse flips all 32 bits one by one. reverseBytes swaps whole bytes and keeps the bits inside each byte in order.
When do I need reverseBytes? When you convert between big-endian and little-endian byte order, for example when reading binary files or network data from another system.
Does calling it twice give back the original? Yes. Reversing the byte order twice restores the number.
Why does a small number become so large? The set bits move into the top byte. For example, 1 becomes 16777216, and 255 becomes -16777216.
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.
13 people found this page helpful
Helpful Share Copy link Suggestion