500 B
500 B
title, type, language, tags, author, cover, dateModified
| title | type | language | tags | author | cover | dateModified | |
|---|---|---|---|---|---|---|---|
| Number is power of two | snippet | javascript |
|
chalarangelo | flower-portrait-10 | 2020-10-20T23:02:01+03:00 |
Checks if the given number is a power of 2.
- Use the bitwise binary AND operator (
&) to determine ifnis a power of2. - Additionally, check that
nis not falsy.
const isPowerOfTwo = n => !!n && (n & (n - 1)) == 0;
isPowerOfTwo(0); // false
isPowerOfTwo(1); // true
isPowerOfTwo(8); // true