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.

Implementation

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