Skip to content

Latest commit

 

History

History
25 lines (19 loc) · 422 Bytes

check-if-a-number-is-a-power-of-2.mdx

File metadata and controls

25 lines (19 loc) · 422 Bytes
category created title updated
Validator
2020-04-19
Check if a number is a power of 2
2021-10-13

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