Swapping bytes of an integer in Java I have this tiny function that expects an int and returns an int with the bytes swapped:
public class Main {
    
    public static void main(String[] args) {
        int cafeBabe = 0xcafebabe;
        System.out.println(Integer.toHexString(cafeBabe));
        System.out.println(Integer.toHexString(byteSwap(cafeBabe)));
        System.out.println(Integer.toHexString(byteSwap(byteSwap(cafeBabe))));
    }
    
    public static int byteSwap(int a) {
        return ((a &amp; 0xff000000) &gt;&gt;&gt; 24) |
               ((a &amp; 0x00ff0000) &gt;&gt;&gt; 8)  |
               ((a &amp; 0x0000ff00) &lt;&lt; 8)   |
               ((a &amp; 0x000000ff) &lt;&lt; 24);
    }
}

Basically, we are switching between endianess. Now, is there a better/more efficient way of accomplishing the task?
Historical Note
The very first comment on the OP's question -- long before any answers were posted -- was a question asked by myself, and it very pointedly asked:

Without using Integer.reverseBytes(cafeBabe)?

The question was two-fold:

Point out to the OP that a built-in method exists, and is likely the most optimal way of doing this.
Ask if they are trying to reinvent-the-wheel.

That comment/question has been removed.  If the OP responded to it, the response has been deleted as well and I never saw it.
This answer is not advocating the OP reinvent-the-wheel.  The Java JVM / JIT will have implemented the most efficient way of performing the operation, using internal features (such as @HotSpotIntrinsicCandidate) that are not necessarily available to the end-user.
Reinventing the wheel
public static int byteSwap(int a) {
    return ((a &amp; 0xff000000) &gt;&gt;&gt; 24) |
           ((a &amp; 0x00ff0000) &gt;&gt;&gt; 8)  |
           ((a &amp; 0x0000ff00) &lt;&lt; 8)   |
           ((a &amp; 0x000000ff) &lt;&lt; 24);
}

This function uses 6 distinct constants, 4 AND operations, 4 shifts, and 3 OR operations.
There is no point with the AND operation in ((a &amp; 0xff000000) &gt;&gt;&gt; 24) or in ((a &amp; 0x000000ff) &lt;&lt; 24) since the 24 bits which are being masked are immediately shifted out anyway.  This removes 2 constants, and 2 AND operations, which reduces the code to:
public static int byteSwap(int a) {
    return (a &gt;&gt;&gt; 24)               |
           ((a &amp; 0x00ff0000) &gt;&gt;&gt; 8) |
           ((a &amp; 0x0000ff00) &lt;&lt; 8)  |
           (a &lt;&lt; 24);
}

By reworking the order of second operation, we can remove another constant, the 0x00ff0000, and instead reuse the 0xff00 constant.  This eliminates the loading of the third bit mask constant, saving additional JVM byte code instructions:
public static int byteSwap(int a) {
    return (a &gt;&gt;&gt; 24)           |
           ((a &gt;&gt;&gt; 8) &amp; 0xff00) |
           ((a &amp; 0xff00) &lt;&lt; 8)  |
           (a &lt;&lt; 24);
}

This reworked version is very similar to the built-in Integer.reverseBytes() code in the system library:
@HotSpotIntrinsicCandidate
public static int reverseBytes(int i) {
    return (i &lt;&lt; 24)            |
           ((i &amp; 0xff00) &lt;&lt; 8)  |
           ((i &gt;&gt;&gt; 8) &amp; 0xff00) |
           (i &gt;&gt;&gt; 24);
}

I don't know if the difference in ordering gains any additional speed, but (dollars to donuts) the @HotSpotIntrinsicCandidate annotation probably does - so just use the built-in function.