Back to DSA
Reverse Bits
easyGiven a 32-bit unsigned integer, reverse the order of its bits and return the resulting integer.
Examples
Example 1:
Input:
n = 13 (00000000000000000000000000001101)Output:
2952790016 (10110000000000000000000000000000) Example 2:
Input:
n = 1 (00000000000000000000000000000001)Output:
2147483648 (10000000000000000000000000000000)Hints
public class Solution {
public int reverseBits(int n) {
int result = 0;
for (int i = 0; i < 32; i++) {
result = (result << 1) | (n & 1);
n >>= 1;
}
return result;
}
}Time complexity
O(1)Space complexity
O(1)