Sort by

recency

|

776 Discussions

|

  • + 0 comments

    2 line Java hackish solution:

    public static long flippingBits(long n) {
            long maxLong = 4294967295L;
            
            return -1 * (~n ^ maxLong) - 1;
        }
    
  • + 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;
    }
    
  • + 0 comments

    Python Solution

    1. 			MaxNumBin = ''.join('1' for _ in range(32))
    2. 			MaxNumDec = int(MaxNumBin,2)
    3. 			return MaxNumDec - n
    
  • + 0 comments

    Solution in JS:

    function flippingBits(n) {

    return (~n) >>> 0;
    

    }

  • + 0 comments

    A simple attempt using C++ bitset

    long flippingBits(long n) { std::bitset<32> bits(n); for(int i=0;i<32;i++){ bits.flip(i);
    } return bits.to_ullong(); }