Sort by

recency

|

779 Discussions

|

  • + 0 comments

    def flippingBits(n): num = ~n return 4294967296 + num

  • + 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

    Here is my Python solution!

    def binary(num):
        remainders = []
        while num != 0 :
            remainders.insert(0, str(num % 2))
            num //= 2
        return "".join(remainders)
    
    def decimal(num):
        newnum = 0
        for i in range(len(num)):
            newnum += int(num[-i - 1]) * 2 ** i
        return newnum
    
    def flippingBits(n):
        num = binary(n)
        num = "0" * (32 - len(num)) + num
        for i in range(len(num)):
            if num[i] == "0":
                num = num[:i] + "1" + num[i + 1:]
            else:
                num = num[:i] + "0" + num[i + 1:]
        return decimal(num)
    
  • + 0 comments

    long flippingBits(uint32_t n) { return ~n; }

  • + 0 comments

    2 line Java hackish solution:

    public static long flippingBits(long n) {
            long maxLong = 4294967295L;
            
            return -1 * (~n ^ maxLong) - 1;
        }