Power of Two

Determine if a positive integer is a power of two.

bool isPowerOfTwo(int n) {
  if (n <= 0)
    return false

  return !(n & n-1)
}
Scroll to top