Sort by

recency

|

773 Discussions

|

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

  • + 0 comments

    java(8)

        int[] binary = new int[32];
        int count = 0;
        int idx = 31;
    
        while (count < 32) {
    
            binary[idx--] = (int) (n % 2);
            n = n / 2;
            count++;
    
        }
    
        for (int i = 0; i < binary.length; i++) {
            if (binary[i] == 0) {
                binary[i] = 1;
            } else {
                binary[i] = 0;
            }
        }
        long flifResult = 0;
        int p = 0;
        for (int i = 31; i >= 0; i--) {
            flifResult += binary[i] * Math.pow(2, p++);
        }
    
        return flifResult;
    
  • + 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

    No need for complex code. Flipping bits meaning is to just get the biggest number in that requested bit and then substract those with the number asked in the question. For ex. : the question wanted a 32 bit, so the max number would be 4294967295. Now to get what the flipping bits of lets say 10 in 32 bit and return it to decimal, just do 4294967295 - 10 which is 4294967285, and there you go the decimal version of its flipping bit.

    Here is in Go

    func flippingBits(n int64) int64 {
        // Write your code here
        return 4294967295 - n
    }