190. reverse bits

Reverse bits of a given 32 bits unsigned integer.

Example 1:

1
2
3
Input: 00000010100101000001111010011100
Output: 00111001011110000010100101000000
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.

Example 2:

1
2
3
Input: 11111111111111111111111111111101
Output: 10111111111111111111111111111111
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10101111110010110010011101101001.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class  {

public int reverseBits(int n) {
int result = 0;
int i = 0;
while (i < 32) {
int temp = n & 0x01;
n = n >> 1;
result = (result << 1) | temp;
i++;
}
return result;
}
}