Share my solution


  • 0
    L

    /*
    mask = ~0

    mask ^= (mask<<i);

    0xFFFF,FFFF ^ 0xFFFF,0000 => 0x0000,FFFF

    0x0000,FFFF ^ 0x00FF,FF00 => 0x00FF,00FF

    0x00FF,00FF ^ 0x0FF0,0FF0 => 0x0F0F,0F0F
    */

    uint32_t reverseBits(uint32_t n) 
    

    {

        if(!n || !~n) return n;
    
        int mask = ~0;
        
        for(int i=16; i>0; i>>=1)
    
        {
    
            mask ^= (mask <<i);
    
            n = (n & ~mask)>>i | (n & mask) <<i;
    
        }
    
        return n;
    }

Log in to reply
 

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