leetcode_Power of Two_easy

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

题目意思:推断某个数是否是2的幂。

方法:直接进行bit运算,推断是否这个数二进制位里有且仅有一个1。

class Solution {
public:
    bool isPowerOfTwo(int n) {
        int c=0;
        while(n!=0)
        {
            c+=1&n;
            if(c>1)
                return false;
            n>>=1;
        }
        if(c==1)
            return true;
        else
            return false;
    }
};



原文地址:https://www.cnblogs.com/slgkaifa/p/6795118.html