用 O(1) 时间检测整数 n 是否是 2 的幂次。

 位操作

2的幂次数

2  10

4 100

8 1000

16 10000

...

 1 class Solution {
 2     /*
 3      * @param n: An integer
 4      * @return: True or false
 5      */
 6     public boolean checkPowerOf2(int n) {
 7         // write your code here
 8         return ((n > 0) && ((n & (n - 1)) == 0));
 9     }
10 };
原文地址:https://www.cnblogs.com/docted/p/4610066.html