From 0b4a5785361a1f23dbba060127b252cc5e99e0cc Mon Sep 17 00:00:00 2001 From: Gian Vallejos Date: Tue, 5 Sep 2023 16:29:13 -0500 Subject: [PATCH 1/2] Module of Power of Two --- Bit-Manipulation/ModuleOfPowerOfTwo.js | 10 +++++ .../test/ModuleOfPowerOfTwo.test.js | 37 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 Bit-Manipulation/ModuleOfPowerOfTwo.js create mode 100644 Bit-Manipulation/test/ModuleOfPowerOfTwo.test.js diff --git a/Bit-Manipulation/ModuleOfPowerOfTwo.js b/Bit-Manipulation/ModuleOfPowerOfTwo.js new file mode 100644 index 0000000000..30802e858e --- /dev/null +++ b/Bit-Manipulation/ModuleOfPowerOfTwo.js @@ -0,0 +1,10 @@ +/** + * @author: gianvallejos92 + * This script will find the module of S % N using bitmask + * N is power of 2. E.g: 1, 2, 4, 8, 16, 32... + * Reference: https://www.geeksforgeeks.org/compute-modulus-division-by-a-power-of-2-number/ + */ + +export const ModuleOfPowerOfTwo = (s, n) => { + return (s & (n - 1)) +} \ No newline at end of file diff --git a/Bit-Manipulation/test/ModuleOfPowerOfTwo.test.js b/Bit-Manipulation/test/ModuleOfPowerOfTwo.test.js new file mode 100644 index 0000000000..e7b641693a --- /dev/null +++ b/Bit-Manipulation/test/ModuleOfPowerOfTwo.test.js @@ -0,0 +1,37 @@ +import { ModuleOfPowerOfTwo } from '../ModuleOfPowerOfTwo' + +test('Check 7 module 4', () => { + const res = ModuleOfPowerOfTwo(7, 4) + expect(res).toBe(3) +}) + +test('Check 150 module 4', () => { + const res = ModuleOfPowerOfTwo(150, 4) + expect(res).toBe(2) +}) + +test('Check 155 module 2', () => { + const res = ModuleOfPowerOfTwo(155, 2) + expect(res).toBe(1) +}) + +test('Check 150 module 16', () => { + const res = ModuleOfPowerOfTwo(150, 16) + expect(res).toBe(6) +}) + +test('Check 6 module 4', () => { + const res = ModuleOfPowerOfTwo(6, 4) + expect(res).toBe(2) +}) + +test('Check 12 module 8', () => { + const res = ModuleOfPowerOfTwo(12, 8) + expect(res).toBe(4) +}) + +test('Check 10 module 2', () => { + const res = ModuleOfPowerOfTwo(10, 2) + expect(res).toBe(0) +}) + From f6c42bb28d7c26f50f0eb5ff07f52e71139fce72 Mon Sep 17 00:00:00 2001 From: Gian Vallejos Date: Tue, 5 Sep 2023 16:42:20 -0500 Subject: [PATCH 2/2] fix standard style --- Bit-Manipulation/ModuleOfPowerOfTwo.js | 2 +- Bit-Manipulation/test/ModuleOfPowerOfTwo.test.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Bit-Manipulation/ModuleOfPowerOfTwo.js b/Bit-Manipulation/ModuleOfPowerOfTwo.js index 30802e858e..5d83c1571b 100644 --- a/Bit-Manipulation/ModuleOfPowerOfTwo.js +++ b/Bit-Manipulation/ModuleOfPowerOfTwo.js @@ -7,4 +7,4 @@ export const ModuleOfPowerOfTwo = (s, n) => { return (s & (n - 1)) -} \ No newline at end of file +} diff --git a/Bit-Manipulation/test/ModuleOfPowerOfTwo.test.js b/Bit-Manipulation/test/ModuleOfPowerOfTwo.test.js index e7b641693a..9502061afc 100644 --- a/Bit-Manipulation/test/ModuleOfPowerOfTwo.test.js +++ b/Bit-Manipulation/test/ModuleOfPowerOfTwo.test.js @@ -34,4 +34,3 @@ test('Check 10 module 2', () => { const res = ModuleOfPowerOfTwo(10, 2) expect(res).toBe(0) }) -