From f061ae4948ddd0c9cb211d5b592cc40bc7a17b6d Mon Sep 17 00:00:00 2001 From: madhuredra Date: Sun, 1 Oct 2023 20:56:31 +0530 Subject: [PATCH 1/2] feat : added is power of two in bit manipulation --- bit-manipulation/is_power_of_2.ts | 25 +++++++++++++++++++++ bit-manipulation/test/is_power_of_2.test.ts | 14 ++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 bit-manipulation/is_power_of_2.ts create mode 100644 bit-manipulation/test/is_power_of_2.test.ts diff --git a/bit-manipulation/is_power_of_2.ts b/bit-manipulation/is_power_of_2.ts new file mode 100644 index 00000000..487c1a8c --- /dev/null +++ b/bit-manipulation/is_power_of_2.ts @@ -0,0 +1,25 @@ +/** + * This code will check whether the given number is a power of two or not. + * @author dev-madhurendra + * @explanation + + A number will be a power of two if only one bit is set and rest are unset. + This is true for all the cases except 01 because (2^0 = 1) which is not a power of 2. + For eg: 10 (2^1 = 2), 100 (2^2 = 4), 10000 (2^4 = 16) + + @see: https://www.hackerearth.com/practice/notes/round-a-number-to-the-next-power-of-2/ + + If we will subtract 1 from a number that is a power of 2 we will get it's + 1's complement.And we know that 1's complement is just opp. of that number. + So, (n & (n-1)) will be 0. + + For eg: (1000 & (1000-1)) + 1 0 0 0 // Original Number (8) + 0 1 1 1 // After Subtracting 1 (8-1 = 7) + _______ + 0 0 0 0 // will become 0 + * @param {number} + * @returns {boolean} + */ + +export const isPowerOfTwo = (n: number): boolean => n > 0 && (n & (n - 1)) === 0 \ No newline at end of file diff --git a/bit-manipulation/test/is_power_of_2.test.ts b/bit-manipulation/test/is_power_of_2.test.ts new file mode 100644 index 00000000..1276e6d1 --- /dev/null +++ b/bit-manipulation/test/is_power_of_2.test.ts @@ -0,0 +1,14 @@ +import { isPowerOfTwo } from "../is_power_of_2" + + +describe('IsPowerOfTwo' , () => { + it.each([ + [0, false], + [1, true], + [4, true], + [1024, true], + [1025, false], + ])('Check if %i is a power of 2 or not', (number, expected) => { + expect(isPowerOfTwo(number)).toBe(expected); + }); +}) \ No newline at end of file From 3b3e21311fb8d20f160c96db2de6fef93e922aef Mon Sep 17 00:00:00 2001 From: madhuredra Date: Sun, 1 Oct 2023 21:39:37 +0530 Subject: [PATCH 2/2] fix : file name fixed --- {bit-manipulation => bit_manipulation}/is_power_of_2.ts | 0 {bit-manipulation => bit_manipulation}/test/is_power_of_2.test.ts | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {bit-manipulation => bit_manipulation}/is_power_of_2.ts (100%) rename {bit-manipulation => bit_manipulation}/test/is_power_of_2.test.ts (100%) diff --git a/bit-manipulation/is_power_of_2.ts b/bit_manipulation/is_power_of_2.ts similarity index 100% rename from bit-manipulation/is_power_of_2.ts rename to bit_manipulation/is_power_of_2.ts diff --git a/bit-manipulation/test/is_power_of_2.test.ts b/bit_manipulation/test/is_power_of_2.test.ts similarity index 100% rename from bit-manipulation/test/is_power_of_2.test.ts rename to bit_manipulation/test/is_power_of_2.test.ts