isPowerOfTwo function

bool isPowerOfTwo(
  1. int n
)

Returns true if n is a positive power of two (1, 2, 4, 8, …).

Returns false for zero and negative numbers. Audited: 2026-06-12 11:26 EDT

Implementation

bool isPowerOfTwo(int n) => n > 0 && (n & (n - 1)) == 0;