My solution using mask xor

  • 0
    uint32_t reverseBits(uint32_t n) {
        for(int i=0; i<16; i++)
            uint32_t mask = 1<<i | 1<<(31-i); // mask on ith and (31-i)th bit
            if(n & mask && mask != (n & mask)) //if ith and (31-i)th differ togle them
                n ^= mask;
        return n;

Log in to reply

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