加州提子面包

JavaScript算法试题系列-10

问题描述:

1
2
3
4
5
// 数字判断是否为 2 的指数值isPowerOfTwo(4); // true
isPowerOfTwo(64); // true
isPowerOfTwo(1); // true
isPowerOfTwo(0); // false
isPowerOfTwo(-1); // false

解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// For the non-zero case:
function isPowerOfTwo(number) {
// `&` uses the bitwise n.
// In the case of number = 4; the expression would be identical to:
// `return (4 & 3 === 0)`
// In bitwise, 4 is 100, and 3 is 011. Using &, if two values at the same
// spot is 1, then result is 1, else 0. In this case, it would return 000,
// and thus, 4 satisfies are expression.
// In turn, if the expression is `return (5 & 4 === 0)`, it would be false
// since it returns 101 & 100 = 100 (NOT === 0)
return number & (number - 1) === 0;
}
// For zero-case:
function isPowerOfTwoZeroCase(number) {
return (number !== 0) && ((number & (number - 1)) === 0);
}
------ 本文结束 ------