Determine if a positive integer is a power of two.
bool isPowerOfTwo(int n) {
if (n <= 0)
return false;
return !(n & (n - 1));
}
Explanation
- This function checks if a given positive integer
nis a power of two. - It utilizes a bitwise operation to confirm whether
nhas only one bit set in its binary representation.