Given an integer, write a function to determine if it is a power of two. 
意:推断一个数是否是2的n次幂
假设一个数小于或等于0。一定不是2的幂次数 
假设一个大于0且数是2的n次幂,则其的二进制形式有且仅有一个1,反之成立。
public class Solution {
    public boolean isPowerOfTwo(int n) {
        if(n<=0)
            return false;
        int i = 0;
        int countBit = 0;
        while(i < 32){
            if((n&(1<<i))!=0)
                countBit++;
            i++;
        }
        if(countBit != 1)
            return false;
        return true;
    }
}
T(n)=O(1)
public static void main(String [] args){
        int n = 4;
        Solution s = new Solution();    
        System.out.println(s.isPowerOfTwo(n));
    }
true
原文:http://www.cnblogs.com/mengfanrong/p/5425129.html