Flipping bits

Sort by

recency

|

163 Discussions

|

  • + 0 comments

    i mean we can just return ~n & 0xFFFFFFFF.........thats it....you are all set

  • + 0 comments
    public static long flippingBits(long n) {
            // Create a mask with all bits set to 1
            long mask = 0xFFFFFFFFL; 
    
            // Flip all bits using XOR operation
            return n ^ mask;
        }
    
  • + 0 comments

    Java:

    public static long flippingBits(long n) { // Write your code here return ~n & 0xFFFFFFFFL; }

  • + 0 comments
    function flippingBits(n) {
        // Write your code here
        const maxBits = (2 ** 32) - 1
        return maxBits - n;
    }
    
  • + 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;
    
        }