342. Power of Four

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?

含义:判断一个数是否为4的n次方

方法一:     http://blog.csdn.net/yums467/article/details/51425679  //换底公式

1     public boolean isPowerOfFour(int num) {
2         if(num < 1) return false;
3         if(num == 1) return true;
4         int n = (int) (Math.log10(num)/Math.log10(4));
5         return Math.pow(4,n) == num;
6     }

方法二: num & (num - 1)用来判断num是否是2的次方 4的次方数减一,能被3整除

1     public boolean isPowerOfFour(int num) {
2         if(num < 1) return false;
3         if(num == 1) return true;
4         return num > 0 && (num & (num - 1)) == 0 && (num - 1) % 3 == 0; 
5     }
原文地址:https://www.cnblogs.com/wzj4858/p/7727718.html