From eafa1b541b29625b7cdeb491a41d28e68dfe9ec3 Mon Sep 17 00:00:00 2001 From: Nikhil Gautam <89832530+nikgautamgithub@users.noreply.github.com> Date: Tue, 3 Oct 2023 23:13:38 +0530 Subject: [PATCH 1/7] Create minMaxElement.js --- Bit-Manipulation/minMaxElement.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Bit-Manipulation/minMaxElement.js diff --git a/Bit-Manipulation/minMaxElement.js b/Bit-Manipulation/minMaxElement.js new file mode 100644 index 0000000000..c3874e9590 --- /dev/null +++ b/Bit-Manipulation/minMaxElement.js @@ -0,0 +1,17 @@ +/** + * @author : nikgautamgithub + * + * Find max or min element using bit operators. + * @param {number} x - The input number to compare. + * @param {number} y - The input number to compare. + * @returns Min/Max Element + * + * @example + * const maxi = max(5,3) // Returns max number i.e. 5 + * const mini = min(5,3); // Returns min number i.e. 3 + */ +const max = (x, y) => x ^ ((x ^ y) & -(x < y ? 1 : 0)); + +const min = (x, y) => y ^ ((x ^ y) & -(x < y ? 1 : 0)); + +export { min, max }; From d0c6b9f0b3d796326a0e0957c42d575777d87010 Mon Sep 17 00:00:00 2001 From: Nikhil Gautam <89832530+nikgautamgithub@users.noreply.github.com> Date: Tue, 3 Oct 2023 23:16:56 +0530 Subject: [PATCH 2/7] Create minMaxElement.test.js --- Bit-Manipulation/test/minMaxElement.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Bit-Manipulation/test/minMaxElement.test.js diff --git a/Bit-Manipulation/test/minMaxElement.test.js b/Bit-Manipulation/test/minMaxElement.test.js new file mode 100644 index 0000000000..f65c5f88da --- /dev/null +++ b/Bit-Manipulation/test/minMaxElement.test.js @@ -0,0 +1,9 @@ +import { min,max } from '../minMaxElement' + +test('Find max element from 5 and 3 :', () => { + expect(max(5,3)).toBe(5) +}) + +test('Find min element from 5 and 3 :', () => { + expect(min(5,3).toBe(3) +}) From b036dba682a3404e1689bfaee67c01b3d0c8bc0e Mon Sep 17 00:00:00 2001 From: Nikhil Gautam <89832530+nikgautamgithub@users.noreply.github.com> Date: Tue, 3 Oct 2023 23:32:00 +0530 Subject: [PATCH 3/7] Rename minMaxElement.js to MinMaxElement.js --- Bit-Manipulation/{minMaxElement.js => MinMaxElement.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Bit-Manipulation/{minMaxElement.js => MinMaxElement.js} (100%) diff --git a/Bit-Manipulation/minMaxElement.js b/Bit-Manipulation/MinMaxElement.js similarity index 100% rename from Bit-Manipulation/minMaxElement.js rename to Bit-Manipulation/MinMaxElement.js From 60f09ddc042800ff2ec66d0a3c935cabd4abff37 Mon Sep 17 00:00:00 2001 From: Nikhil Gautam <89832530+nikgautamgithub@users.noreply.github.com> Date: Tue, 3 Oct 2023 23:32:47 +0530 Subject: [PATCH 4/7] Update and rename minMaxElement.test.js to MinMaxElement.test.js --- .../test/{minMaxElement.test.js => MinMaxElement.test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Bit-Manipulation/test/{minMaxElement.test.js => MinMaxElement.test.js} (100%) diff --git a/Bit-Manipulation/test/minMaxElement.test.js b/Bit-Manipulation/test/MinMaxElement.test.js similarity index 100% rename from Bit-Manipulation/test/minMaxElement.test.js rename to Bit-Manipulation/test/MinMaxElement.test.js From 99e3187468b7e5b6f9669757fbbb60bec49c1870 Mon Sep 17 00:00:00 2001 From: Nikhil Gautam <89832530+nikgautamgithub@users.noreply.github.com> Date: Tue, 3 Oct 2023 23:36:10 +0530 Subject: [PATCH 5/7] Update MinMaxElement.test.js --- Bit-Manipulation/test/MinMaxElement.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bit-Manipulation/test/MinMaxElement.test.js b/Bit-Manipulation/test/MinMaxElement.test.js index f65c5f88da..33623e2ad8 100644 --- a/Bit-Manipulation/test/MinMaxElement.test.js +++ b/Bit-Manipulation/test/MinMaxElement.test.js @@ -5,5 +5,5 @@ test('Find max element from 5 and 3 :', () => { }) test('Find min element from 5 and 3 :', () => { - expect(min(5,3).toBe(3) + expect(min(5,3)).toBe(3) }) From 5c7099d664803e4dbc8a65879fa20049c3774f96 Mon Sep 17 00:00:00 2001 From: Nikhil Gautam <89832530+nikgautamgithub@users.noreply.github.com> Date: Tue, 3 Oct 2023 23:39:13 +0530 Subject: [PATCH 6/7] Update MinMaxElement.test.js --- Bit-Manipulation/test/MinMaxElement.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Bit-Manipulation/test/MinMaxElement.test.js b/Bit-Manipulation/test/MinMaxElement.test.js index 33623e2ad8..8a45d779d3 100644 --- a/Bit-Manipulation/test/MinMaxElement.test.js +++ b/Bit-Manipulation/test/MinMaxElement.test.js @@ -1,9 +1,9 @@ -import { min,max } from '../minMaxElement' +import { min, max } from '../minMaxElement' test('Find max element from 5 and 3 :', () => { - expect(max(5,3)).toBe(5) + expect(max(5, 3)).toBe(5) }) test('Find min element from 5 and 3 :', () => { - expect(min(5,3)).toBe(3) + expect(min(5, 3)).toBe(3) }) From bc881073d1f403e292c684c59d20efd8d9193ccf Mon Sep 17 00:00:00 2001 From: Nikhil Gautam <89832530+nikgautamgithub@users.noreply.github.com> Date: Tue, 3 Oct 2023 23:41:00 +0530 Subject: [PATCH 7/7] Update MinMaxElement.js --- Bit-Manipulation/MinMaxElement.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Bit-Manipulation/MinMaxElement.js b/Bit-Manipulation/MinMaxElement.js index c3874e9590..54277128a2 100644 --- a/Bit-Manipulation/MinMaxElement.js +++ b/Bit-Manipulation/MinMaxElement.js @@ -10,8 +10,8 @@ * const maxi = max(5,3) // Returns max number i.e. 5 * const mini = min(5,3); // Returns min number i.e. 3 */ -const max = (x, y) => x ^ ((x ^ y) & -(x < y ? 1 : 0)); +const max = (x, y) => x ^ ((x ^ y) & -(x < y ? 1 : 0)) -const min = (x, y) => y ^ ((x ^ y) & -(x < y ? 1 : 0)); +const min = (x, y) => y ^ ((x ^ y) & -(x < y ? 1 : 0)) -export { min, max }; +export { min, max }