Flipping bits

Sort by

recency

|

348 Discussions

|

  • + 0 comments

    def flippingBits(n): return n ^ 0xFFFFFFFF # XOR with a 32-bit mask

  • + 0 comments

    Java: long max32unsignedbit = (1L<<32)-1; return max32unsignedbit^n;

  • + 0 comments

    Javscript solution

    • ~ Flips all the bits of the input n.
    • >>> Ensures the result is treated as an unsigned 32-bit integer
    function flippingBits(n) {
        // Write your code here
        return ~n  >>> 0
    }
    
  • + 1 comment

    Python solution using XOR operator

    def flippingBits(n):
        return  n ^ (2**32 - 1)
    
  • + 0 comments

    My Rust approach

    fn flippingBits(n: i64) -> i64 {
        !(n as u32) as i64
    }