C++ Solution using Bit Manipulation with one line code


  • 3
    Y
    /*
    * check given integer is positive or not && check whether it is power of two 
    * && check whether the only one bit is on the odd position or not: 
     * (1431655765) <==> 1010101010101010101010101010101  
    */ 
    return num > 0 && !(num&(num-1)) && ((num&1431655765)==num);

Log in to reply
 

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