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 {
public boolean isPowerOfFour(int num) {
// double res=Math.log(num)/Math.log(4);
// return Math.abs((res-(int)res))<0.0000000001;
if(num<0)
return false;
return (num&(num-1))==0&&(num&0x55555555)==num;//1个数是4的n次方一定是2的n次方,但是反过来不一定成立//所以判断4的n次方还得再加一个条件,4的幂只能是奇数位为1,而2的幂只有有一个位置为1就行
//还有一个小tips,如果nums是2的n次方,那么nums&(nums-1)=0
}
}