leetcode题解-231-2的幂

题目

1
2
3
4
5
6
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && !(n&(n-1));
}
};

考虑:

  • num > 0时,才会是一个数的幂。负数和0不会是一个正整数的幂。

  • !(num&(num-1))时,用来判断num是不是2的幂,

    • 因为一个数是2的幂的话,则它2进制的第一位必然是1,与num-1与运算后,结果必然为0

Reference

https://leetcode-cn.com/problems/power-of-four/solution/e-you-shi-yi-dao-zhuang-bi-jie-fa-de-suan-fa-ti-2/

https://www.geeksforgeeks.org/program-to-find-whether-a-no-is-power-of-two/