diff --git a/src/algorithms/math/bits/README.md b/src/algorithms/math/bits/README.md index 4c5c5148..b432043f 100644 --- a/src/algorithms/math/bits/README.md +++ b/src/algorithms/math/bits/README.md @@ -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. -#### 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 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. +#### 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 diff --git a/src/algorithms/math/bits/__test__/ifPowerOf2.test.js b/src/algorithms/math/bits/__test__/ifPowerOf2.test.js deleted file mode 100644 index 564abc20..00000000 --- a/src/algorithms/math/bits/__test__/ifPowerOf2.test.js +++ /dev/null @@ -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); - }); -}); diff --git a/src/algorithms/math/bits/__test__/isPowerOfTwo.test.js b/src/algorithms/math/bits/__test__/isPowerOfTwo.test.js new file mode 100644 index 00000000..3408f2c1 --- /dev/null +++ b/src/algorithms/math/bits/__test__/isPowerOfTwo.test.js @@ -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); + }); +}); diff --git a/src/algorithms/math/bits/ifPowerOf2.js b/src/algorithms/math/bits/ifPowerOf2.js deleted file mode 100644 index dd29f9f2..00000000 --- a/src/algorithms/math/bits/ifPowerOf2.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * @param {number} number - * @return bool - */ -export default function ifPowerOf2(number) { - return number && (!(number & (number - 1))); -} diff --git a/src/algorithms/math/bits/isPowerOfTwo.js b/src/algorithms/math/bits/isPowerOfTwo.js new file mode 100644 index 00000000..28e3e673 --- /dev/null +++ b/src/algorithms/math/bits/isPowerOfTwo.js @@ -0,0 +1,7 @@ +/** + * @param {number} number + * @return bool + */ +export default function isPowerOfTwo(number) { + return (number & (number - 1)) === 0; +}