190. Reverse Bits

Solution

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t result = 0;
    
        for (int i = 0; i < 32; i++) {
            int bit = (n >> i) & 1;
            result = result | (bit << (31 - i));
        }

        return result;
    }
};

Complexity

  • Time Complexity: O(N), where N is the number of bits in n (32).
  • Space Complexity: O(1).

All Solutions