Flipping bits

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