diff --git a/Bit-Manipulation/IsPowerofFour.js b/Bit-Manipulation/IsPowerofFour.js new file mode 100644 index 0000000000..e1f61c63d3 --- /dev/null +++ b/Bit-Manipulation/IsPowerofFour.js @@ -0,0 +1,24 @@ +/* + author: @dev-madhurendra + + This script will check whether the given + number is a power of four or not. + + A number will be power of four if and only if there is a 1 in the binary + of the digit and it will be at the first position + + n n-1%3==0 (n-1)bin (n)bin + 1 0 0 1 - 4^0 + 4 3 011 100 - 4^1 + 16 15 01111 10000 - 4^2 + 64 63 011111 1000000 - 4^3 + + a) There is only one bit set in the binary representation of n (or n is a power of 2) + b) There is only one bit unset in the n-1 + c) And the and of n&n-1 will be 0 + d) N-1 will be divisble by 3 +*/ + +const IsPowerOfFour = (n) => ((n > 0) && ((n & n - 1) === 0) && (n % 3 === 1)) + +export { IsPowerOfFour } diff --git a/Bit-Manipulation/test/IsPowerOfFour.test.js b/Bit-Manipulation/test/IsPowerOfFour.test.js new file mode 100644 index 0000000000..18e16fde76 --- /dev/null +++ b/Bit-Manipulation/test/IsPowerOfFour.test.js @@ -0,0 +1,14 @@ +import { IsPowerOfFour } from '../IsPowerofFour' + +describe('IsPowerOfFour', () => { + it.each([ + [0, false], + [4, true], + [16, true], + [12, false], + [64, true], + [-64, false] + ])('should return the number is power of four or not', (n, expected) => { + expect(IsPowerOfFour(n)).toBe(expected) + }) +})