```
public int reverseBits(int n) {
for(int i = 0; i < 16; i ++) {
n = swapBits(n, i, 32 - i - 1);
}
return n;
}
private int swapBits(int n, int i, int j) {
int lower = (n >> i) & 1;
int higher = (n >> j) & 1;
if((lower ^ higher) != 0) { // if the first bit is not equal to the last bit, swap them.
return n ^= (1 << i) | (1 << j);
}
return n;
}
```