Flipping bits

Sort by

recency

|

341 Discussions

|

  • + 0 comments

    JAVA code

    public static long flippingBits(long n) {
    // Write your code here
    

    return n ^ 0xFFFFFFFFL; }

  • + 0 comments

    c++ solution. The key is that NOT operation on bits does the 'flipping'. And since we operate on unsigned positive ints, gotta add 2 times INTMAX+1 to avoid negative numbers.

    long flippingBits(long n) {
        
        return ~n+2l*INT32_MAX+2;
    }
    
  • + 0 comments
        return n^int("1"*32,2)
    
  • + 0 comments

    C makes this trivial using the XOR operator: return(UINT32_MAX^n);

  • + 0 comments

    C++ Solution

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