mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-27 20:11:53 +08:00
Add isPowerOfTwo functions.
This commit is contained in:
31
src/algorithms/math/is-power-of-two/isPowerOfTwoBitwise.js
Normal file
31
src/algorithms/math/is-power-of-two/isPowerOfTwoBitwise.js
Normal file
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @param {number} number
|
||||
* @return {boolean}
|
||||
*/
|
||||
export default function isPowerOfTwoBitwise(number) {
|
||||
// Don't work with negative numbers.
|
||||
if (number < 0) {
|
||||
throw new Error('Please provide positive number');
|
||||
}
|
||||
|
||||
// 0 and 1 are not powers of two.
|
||||
if (number <= 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Powers of two in binary look like this:
|
||||
* 1: 0001
|
||||
* 2: 0010
|
||||
* 4: 0100
|
||||
* 8: 1000
|
||||
*
|
||||
* Note that there is always exactly 1 bit set. The only exception is with a signed integer.
|
||||
* e.g. An 8-bit signed integer with a value of -128 looks like:
|
||||
* 10000000
|
||||
*
|
||||
* So after checking that the number is greater than zero, we can use a clever little bit
|
||||
* hack to test that one and only one bit is set.
|
||||
*/
|
||||
return (number & (number - 1)) === 0;
|
||||
}
|
Reference in New Issue
Block a user