leetcode 231. Power of Two

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

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

 

posted on 2017-08-05 22:33  Beserious  阅读(141)  评论(0编辑  收藏  举报