mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
Add ModuloPowerOfTwo
algorithm (#5863)
This commit is contained in:
@ -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)
|
||||
@ -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)
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user