c# solution


  • 0
    C
    public uint reverseBits(uint n)
    {
        int no_of_bits = 32;
    
        for (int i = 0; i < no_of_bits / 2; ++i)
        {
            int right = i;
            int left = no_of_bits - i - 1;
    
            uint leftBit = (n >> left) & 1;
            uint rightBit = (n >> right) & 1;
    
            if (rightBit == 1)
                n |= 1u << left;
            else
                n &= ~(1u << left);
    
            if (leftBit == 1)
                n |= 1u << right;
            else
                n &= ~(1u << right);
        }
    
        return n;
    }
    

Log in to reply
 

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