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 n is a power of two.
  • It utilizes a bitwise operation to confirm whether n has only one bit set in its binary representation.