Back to DSA

Reverse Bits

easy
Acceptance: 55%
Bit Manipulation

Given 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

00:00
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 complexityO(1)
Space complexityO(1)