Flipping bits

Sort by

recency

|

159 Discussions

|

  • + 0 comments
    public static long flippingBits(long n) {
        // Write your code here
        long flip = 0;
        for(int i = 31; i>=0 ; i--){
            if(n- Math.pow(2,i)>=0){
                n-=Math.pow(2,i);
            }
            else{
                flip+=Math.pow(2,i);
            }
        }
        return flip;
    
        }
    
  • + 0 comments
    def flippingBits(n):
        num_bits = 32
        mask = (1 << num_bits) - 1
        ans = mask - n
        return ans
    
  • + 0 comments
    def flippingBits(n):
        # Write your code here
        return n ^ (2**32 - 1)
    
  • + 0 comments

    just
    return 4294967295-n

    I don't know what the point of this question is.

  • + 0 comments

    My Typescript solution:

    function flippingBits(n: number): number {
        let splitBinary = n.toString(2).split("");
        let base2 = new Uint32Array(32);
        
        const sbLength = splitBinary.length;
        
        for (let i = 0; i < 32; i++) {
            const sbIndexInverse = 32 - i;
            if (sbIndexInverse <= sbLength) {
                base2[i] = Number(splitBinary[sbLength - sbIndexInverse])
            }
            base2[i] = base2[i] === 0 ? 1 : 0;
        }
    
        return parseInt(base2.join(""), 2);
    }