Power of Two

Given an integer, write a function to determine if it is a power of two.

//初始理解为把数字拆分为两个整数的乘积。。。英语差真操蛋

C++:

class Solution {
public:
    bool isPowerOfTwo(int n) {
        
        if(0==n) return false;
        else if(1==n) return true;
    
        while(!(n%2))
        {
            n=n/2;
            if(1==n) return true;
        }
        
        return false;
    }
};

C:

if(0==n) return false;
    else if(1==n) return true;
    
    while(!(n%2))
    {
        n=n/2;
        if(1==n) return true;
    }
        
    return false;

还有一种做法就是根据数字位数判断,不过倾向于秀技巧了,这里就不再探索了

原文地址:https://www.cnblogs.com/jason1990/p/4640882.html