Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example:
Given num = 16, return true. Given num = 5, return false.
Follow up: Could you solve it without loops/recursion?
public class Solution {
/**
* 1. check whether is power of 2
* 2. check the odd number of bit is 1.
*/
public boolean isPowerOfFour(int num) {
if( (num&(num-1)) != 0 )
{
return false;
}
if( (num&0x55555555)==0 )
{
return false;
}
<span style="white-space:pre"> </span>return true;
}
}