• + 0 comments

    Here is my c++ solution, you can watch the video explanation here : https://youtu.be/eZ0lTIzOjhQ

    V1

    long flippingBits(long n) {
        long result = 0;
        for(int i = 31; i >= 0; i--) {
            if(n >= pow(2, i)){
                n -= pow(2,i);
            }
            else result += pow(2, i);
        }
        return result;
    }
    

    V2

    long flippingBits(long n) {
        return 4294967295 ^ n;
    }