Skip to content

feat: Add ModuloPowerOfTwo new algorithm with Junit tests #5863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -731,6 +732,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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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",
"36, 5, 4",
})
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());
}
}