# A 8ms Straight-forward Bit Manipulation C++ Solution

• ``````class Solution {
public:
bool isPowerOfTwo(int n) {
if(n < 0) return false;
int count = 0;
for(int i = 31; i >=0 ;i--)
if(((1 << i)&n) != 0) count++;
return count == 1;

}
};``````

• What's "straight-forward" about a `continue` that doesn't do anything and that complicated return value handling?

``````bool isPowerOfTwo(int n) {
if(n < 0) return false;
int count = 0;
for(int i = 31; i >=0 ;i--)
count += (n >> i) & 1;
return count == 1;
}``````

• Yes, you are right. Thank you for your suggestion.
The straight-forward just means check each bit directly.

• ``````class Solution { //8ms
public:
bool isPowerOfTwo(int n)
{
if (n < 0 ) return false;
while (n > 1)
n = DividedByTwo (n);
if (n == 1) return true;
if (n == 0) return false;
}
private:
int DividedByTwo (int n)
{
int quo; int rem;
quo = n/2; rem = n%2;
if (rem != 0) return 0;
if (rem == 0) return quo;
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.