Check if a number is a power of 2
JavaScript
const isPowerOfTwo = (n) => (n & (n - 1)) === 0TypeScript
const isPowerOfTwo = (n: number): boolean => (n & (n - 1)) === 0Examples
isPowerOfTwo(256) // true
isPowerOfTwo(129) // falseconst isPowerOfTwo = (n) => (n & (n - 1)) === 0const isPowerOfTwo = (n: number): boolean => (n & (n - 1)) === 0isPowerOfTwo(256) // true
isPowerOfTwo(129) // false