Use bit manipulation to reverse the 32-bit representation.
(n & 1)& (AND), << (left shift), >> (right shift), | (OR)1public class Solution {
2 public int reverseBits(int n) {
3 int result = 0;
4
5 for (int i = 0; i < 32; i++) {
6 // Get the rightmost bit of n
7 int bit = n & 1;
8 // Shift result left and add the bit
9 result = (result << 1) | bit;
10 // Shift n right to process next bit
11 n >>>= 1;
12 }
13
14 return result;
15 }
16}