程式語言 - LeetCode - C++ - 231. Power of Two



題目:


方法:

只有一個 bit 是 1
1      -> 2^0
10     -> 2^1
100    -> 2^2
1000   -> 2^3

解答:

class Solution {
public:
    bool isPowerOfTwo(int n) {
        return n > 0 && (n & (n - 1)) == 0;
    }
};