From 2dc9caf976ce9e1ae93d5a97aa0686144f863304 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 16 Oct 2024 09:07:17 +0530 Subject: [PATCH 1/3] feat: Add `ModuloPowerOfTwo` new algorithm with Junit tests --- .../bitmanipulation/ModuloPowerOfTwo.java | 28 ++++++++++++++ .../bitmanipulation/ModuloPowerOfTwoTest.java | 37 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwo.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java diff --git a/src/main/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwo.java b/src/main/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwo.java new file mode 100644 index 000000000000..537a046f77e4 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwo.java @@ -0,0 +1,28 @@ +package com.thealgorithms.bitmanipulation; + +/** + * This class provides a method to compute the remainder + * of a number when divided by a power of two (2^n) + * without using division or modulo operations. + * + * @author Hardvan + */ +public final class ModuloPowerOfTwo { + private ModuloPowerOfTwo() { + } + + /** + * Computes the remainder of a given integer when divided by 2^n. + * + * @param x the input number + * @param n the exponent (power of two) + * @return the remainder of x divided by 2^n + */ + public static int moduloPowerOfTwo(int x, int n) { + if (n <= 0) { + throw new IllegalArgumentException("The exponent must be positive"); + } + + return x & ((1 << n) - 1); + } +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java b/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java new file mode 100644 index 000000000000..e6cd8923c608 --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java @@ -0,0 +1,37 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class ModuloPowerOfTwoTest { + + @ParameterizedTest + @CsvSource({ + "10, 3, 2", + "15, 2, 3", + "20, 4, 4", + "7, 1, 1", + "5, 1, 1", + }) + void + testModuloPowerOfTwo(int x, int n, int expected) { + assertEquals(expected, ModuloPowerOfTwo.moduloPowerOfTwo(x, n)); + } + + @ParameterizedTest + @CsvSource({ + "10, 0", + "15, -2", + "20, -4", + "7, -1", + "5, -1", + }) + void + testNegativeExponent(int x, int n) { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> ModuloPowerOfTwo.moduloPowerOfTwo(x, n)); + assertEquals("The exponent must be positive", exception.getMessage()); + } +} From 62f8a00885ea94af51724e30bfb37a4d2fefd7b4 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Wed, 16 Oct 2024 03:37:34 +0000 Subject: [PATCH 2/3] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6429757179e9..acb13c96f209 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -39,6 +39,7 @@ * [IsEven](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsEven.java) * [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java) * [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java) + * [ModuloPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwo.java) * [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java) * [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java) * [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java) @@ -702,6 +703,7 @@ * [IsEvenTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsEvenTest.java) * [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java) * [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java) + * [ModuloPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java) * [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java) * [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java) * [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java) From 4d718912e6349010a4f925560b23837dc161ba34 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 16 Oct 2024 09:11:28 +0530 Subject: [PATCH 3/3] Fix --- .../com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java b/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java index e6cd8923c608..ff7f621a64c0 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java @@ -15,6 +15,7 @@ class ModuloPowerOfTwoTest { "20, 4, 4", "7, 1, 1", "5, 1, 1", + "36, 5, 4", }) void testModuloPowerOfTwo(int x, int n, int expected) {