One line C implementation of "Power of Four"


  • 2
    L
        /*
         * 1. 0 is not
         * 2. If the number is power of four, the odd bits(suppose the least significant bit is 0) must be 0;
         * 3. If the number is power of four, there must be only one bit whose value is 1. This method  is from the book <<Hacker's Delight>>
         */
        
       bool isPowerOfFour(int num) {
        	return ( (num!=0) && (num&0xaaaaaaaa) == 0) && ( (num&(num-1)) == 0);
       }

Log in to reply
 

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