标签:its family pow font orm add als math lang
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?
方法1:使用换底公式,跟Power of three同理
public class Solution {
public bool IsPowerOfFour(int num) {
return (num > 0 && (int)(Math.Log10(num) / Math.Log10(4)) - Math.Log10(num) / Math.Log10(4) == 0);
}
}
class Solution {
public:
bool isPowerOfFour(int num) {
return num > 0 && !(num & (num - 1)) && (num - 1) % 3 == 0;
}
};
class Solution {
public:
bool isPowerOfFour(int num) {
return num > 0 && !(num & (num - 1)) && (num & 0x55555555) == num;
}
};
标签:its family pow font orm add als math lang
原文地址:http://www.cnblogs.com/xiejunzhao/p/e8f0b9357aabfe637e4d41ab0079c2c8.html