Skip to content

Latest commit

 

History

History
18 lines (18 loc) · 323 Bytes

single38.md

File metadata and controls

18 lines (18 loc) · 323 Bytes

Power of Two

问题分析:

给定一个整数,写一个函数判断它是否是2的若干次幂。

编程实现:

class Solution {
public:
    bool isPowerOfTwo(int n) {
        int cnt=0;
        while(n>0) 
        {
            cnt+=(n&1);
            n>>=1;
        }
        return cnt==1;
    } 
};