Sort by

recency

|

775 Discussions

|

  • + 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(); }

  • + 0 comments

    Very simple solution: First perform NOT operation then Mask the number with 32-bit 1's (0xFFFF_FFFF) def FlippingBits(n): a = ~n & 0xFFFFFFFF return a