C++ 4ms solution


  • 0
    P
    uint32_t reverseBits(uint32_t n) {
            if ( n == 0 ) return n;
        
            uint32_t returnNum = 0;
            uint32_t bitLeft = 31;
            uint32_t bitRight = 0;
            
            while ( bitLeft >= bitRight )
            {
                returnNum |= ( ( ( ( 1 << bitRight ) & n ) >> bitRight ) << bitLeft );
                returnNum |= ( ( ( ( 1 << bitLeft ) & n ) >> bitLeft ) << bitRight );
                
                --bitLeft;
                ++bitRight;
            }
            
            
            return returnNum;
        }
    

Log in to reply
 

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