10.Power of Two-Leetcode

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

class Solution {
public:
    bool isPowerOfTwo(int n) {
        while(n)
        {
            if(n%2==0)n=n/2;//不断除2,看能否最终得到1
            else break;
        }
        if(n==1)return true;
        else return false;
    }
};
原文地址:https://www.cnblogs.com/freeopen/p/5483013.html