Reverse Bits

Reverse the bits of an 32 bit unsigned integer A.
Thougts
  • 0:00 Sounds like an easy question I should be able to solve in 2 minutes.

  • 7:30 Ok, it was easy enough. DONE.

inline bool isSet(const unsigned int &A, const int &pos) {
    return A & (1 << (pos - 1));
}

inline void setBit(unsigned int &num, const int &pos) {
    num = num | (1 << (32 - pos));
}

unsigned int Solution::reverse(unsigned int A) {
    unsigned int res = 0;
    
    for(int pos = 1; pos <= 32; pos++) 
        if(isSet(A, pos))
            setBit(res, pos);
            
    return res;
}

Short Version:

unsigned int Solution::reverse(unsigned int A) {
    unsigned int res = 0;
    
    for(int pos = 1; pos <= 32; pos++) 
        if(A & (1 << (pos - 1)))
            res = res | (1 << (32 - pos));
            
    return res;
}

Time Complexity: O(1)O(1)

Space Complexity: O(1)O(1)​

Last updated