One Line Java using XOR


  • 0
    B
        return num^((Integer.highestOneBit(num)<<1) - 1);
    

    (Integer.highestOneBit(num)<<1) - 1) --> gives all ones for a given number

    example : num = 5

    101 ^ 111 gives 010 which is 2


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.