mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Add isPowerOfTwo function.
This commit is contained in:
parent
70b0e0a652
commit
b9d0d9ab38
@ -91,23 +91,6 @@ inverting all of the bits of the number and adding 1 to it.
|
|||||||
|
|
||||||
> See [switchSign.js](switchSign.js) for further details.
|
> See [switchSign.js](switchSign.js) for further details.
|
||||||
|
|
||||||
#### Power of 2
|
|
||||||
|
|
||||||
This method checks if a number provided is power of two. It uses the property that when
|
|
||||||
a power of 2 is `&` with power of 2 minus 1, it would return 0 implying that the provided
|
|
||||||
number is power of 2.
|
|
||||||
|
|
||||||
```
|
|
||||||
Number: 4
|
|
||||||
Power of 2: True
|
|
||||||
|
|
||||||
Number: 1
|
|
||||||
Power of 2: False
|
|
||||||
```
|
|
||||||
|
|
||||||
> See `ifPowerof2` function for further details.
|
|
||||||
|
|
||||||
|
|
||||||
#### Multiply Two Numbers
|
#### Multiply Two Numbers
|
||||||
|
|
||||||
This method multiplies two integer numbers using bitwise operators.
|
This method multiplies two integer numbers using bitwise operators.
|
||||||
@ -173,6 +156,24 @@ When we shift 1 four times it will become bigger than 5.
|
|||||||
|
|
||||||
> See [bitLength.js](bitLength.js) for further details.
|
> See [bitLength.js](bitLength.js) for further details.
|
||||||
|
|
||||||
|
#### Is Power of Two
|
||||||
|
|
||||||
|
This method checks if a number provided is power of two. It uses the following
|
||||||
|
property. Let's say that `powerNumber` is a number that has been formed as a power
|
||||||
|
of two (i.e. 2, 4, 8, 16 etc.). Then if we'll do `&` operation between `powerNumber`
|
||||||
|
and `powerNumber - 1` it will return `0` (in case if number is power of two).
|
||||||
|
|
||||||
|
```
|
||||||
|
Number: 4 = 0b0100
|
||||||
|
Number: 3 = (4 - 1) = 0b0011
|
||||||
|
4 & 3 = 0b0100 & 0b0011 = 0b0000 <-- Equal to zero, is power of two.
|
||||||
|
|
||||||
|
Number: 10 = 0b01010
|
||||||
|
Number: 9 = (10 - 1) = 0b01001
|
||||||
|
10 & 9 = 0b01010 & 0b01001 = 0b01000 <-- Not equal to zero, not a power of two.
|
||||||
|
```
|
||||||
|
|
||||||
|
> See [isPowerOfTwo.js](isPowerOfTwo.js) for further details.
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
import ifPowerOf2 from '../ifPowerOf2';
|
|
||||||
|
|
||||||
describe('ifPowerOf2', () => {
|
|
||||||
it('Should return if the number is power of 2 or not', () => {
|
|
||||||
expect(ifPowerOf2(5)).toBe(false);
|
|
||||||
expect(ifPowerOf2(4)).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
20
src/algorithms/math/bits/__test__/isPowerOfTwo.test.js
Normal file
20
src/algorithms/math/bits/__test__/isPowerOfTwo.test.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import isPowerOfTwo from '../isPowerOfTwo';
|
||||||
|
|
||||||
|
describe('isPowerOfTwo', () => {
|
||||||
|
it('should detect if the number is power of two', () => {
|
||||||
|
expect(isPowerOfTwo(1)).toBe(true);
|
||||||
|
expect(isPowerOfTwo(2)).toBe(true);
|
||||||
|
expect(isPowerOfTwo(3)).toBe(false);
|
||||||
|
expect(isPowerOfTwo(4)).toBe(true);
|
||||||
|
expect(isPowerOfTwo(5)).toBe(false);
|
||||||
|
expect(isPowerOfTwo(6)).toBe(false);
|
||||||
|
expect(isPowerOfTwo(7)).toBe(false);
|
||||||
|
expect(isPowerOfTwo(8)).toBe(true);
|
||||||
|
expect(isPowerOfTwo(9)).toBe(false);
|
||||||
|
expect(isPowerOfTwo(16)).toBe(true);
|
||||||
|
expect(isPowerOfTwo(23)).toBe(false);
|
||||||
|
expect(isPowerOfTwo(32)).toBe(true);
|
||||||
|
expect(isPowerOfTwo(127)).toBe(false);
|
||||||
|
expect(isPowerOfTwo(128)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
@ -1,7 +0,0 @@
|
|||||||
/**
|
|
||||||
* @param {number} number
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
export default function ifPowerOf2(number) {
|
|
||||||
return number && (!(number & (number - 1)));
|
|
||||||
}
|
|
7
src/algorithms/math/bits/isPowerOfTwo.js
Normal file
7
src/algorithms/math/bits/isPowerOfTwo.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* @param {number} number
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
export default function isPowerOfTwo(number) {
|
||||||
|
return (number & (number - 1)) === 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user