C++ Solution | O(1) Time and Space Complexity


#1
bool getBit(int n, int p)
{
    return (n & (1 << p));
}

int updateBit(int n, int p, int x)
{
    return x ? (n | (1 << p)) : (n & ~(1 << p));
}

unsigned int Solution::reverse(unsigned int A) {
    // Do not write main() function.
    // Do not read input, instead use the arguments to the function.
    // Do not print the output, instead return values as specified
    // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details

    int i = 0;
    
    while(i < 16)
    {
        int x = getBit(A, i);
        int y = getBit(A, 31-i);
        
        A = updateBit(A, i, y);
        A = updateBit(A, 31-i, x);
        
        ++i;
    }

    return A;
    ```