C++ Accepted Easy to understand


  • 0
    W

    First shift mask left then right to get each bit in each iteration
    then bind it back to the result in reverse order

    class Solution {
    public:
        uint32_t reverseBits(uint32_t n) {
            uint32_t mask = 0;
            uint32_t res = 0;
            
            for(int i=31; i>=0; i--){
                mask = n;
                mask = mask << i;
                mask = mask >> 31;
                res = res << 1;
                res |= mask;
            }
            
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.