GitHub 6521★

Check if a number is a power of 2

JavaScript version

const isPowerOfTwo = (n) => (n & (n - 1)) === 0;

TypeScript version

const isPowerOfTwo = (n: number): boolean => (n & (n - 1)) === 0;

Examples

isPowerOfTwo(256); // true
isPowerOfTwo(129); // false
Follow me on and to get more useful contents.