File tree Expand file tree Collapse file tree 3 files changed +68
-0
lines changed
main/java/com/thealgorithms/bitmanipulation
test/java/com/thealgorithms/bitmanipulation Expand file tree Collapse file tree 3 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 3939 * [ IsEven] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsEven.java )
4040 * [ IsPowerTwo] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java )
4141 * [ LowestSetBit] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java )
42+ * [ ModuloPowerOfTwo] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwo.java )
4243 * [ NonRepeatingNumberFinder] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java )
4344 * [ NumberAppearingOddTimes] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java )
4445 * [ NumbersDifferentSigns] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java )
731732 * [ IsEvenTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsEvenTest.java )
732733 * [ IsPowerTwoTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java )
733734 * [ LowestSetBitTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java )
735+ * [ ModuloPowerOfTwoTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java )
734736 * [ NonRepeatingNumberFinderTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java )
735737 * [ NumberAppearingOddTimesTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java )
736738 * [ NumbersDifferentSignsTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java )
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .bitmanipulation ;
2+
3+ /**
4+ * This class provides a method to compute the remainder
5+ * of a number when divided by a power of two (2^n)
6+ * without using division or modulo operations.
7+ *
8+ * @author Hardvan
9+ */
10+ public final class ModuloPowerOfTwo {
11+ private ModuloPowerOfTwo () {
12+ }
13+
14+ /**
15+ * Computes the remainder of a given integer when divided by 2^n.
16+ *
17+ * @param x the input number
18+ * @param n the exponent (power of two)
19+ * @return the remainder of x divided by 2^n
20+ */
21+ public static int moduloPowerOfTwo (int x , int n ) {
22+ if (n <= 0 ) {
23+ throw new IllegalArgumentException ("The exponent must be positive" );
24+ }
25+
26+ return x & ((1 << n ) - 1 );
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .bitmanipulation ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5+
6+ import org .junit .jupiter .params .ParameterizedTest ;
7+ import org .junit .jupiter .params .provider .CsvSource ;
8+
9+ class ModuloPowerOfTwoTest {
10+
11+ @ ParameterizedTest
12+ @ CsvSource ({
13+ "10, 3, 2" ,
14+ "15, 2, 3" ,
15+ "20, 4, 4" ,
16+ "7, 1, 1" ,
17+ "5, 1, 1" ,
18+ "36, 5, 4" ,
19+ })
20+ void
21+ testModuloPowerOfTwo (int x , int n , int expected ) {
22+ assertEquals (expected , ModuloPowerOfTwo .moduloPowerOfTwo (x , n ));
23+ }
24+
25+ @ ParameterizedTest
26+ @ CsvSource ({
27+ "10, 0" ,
28+ "15, -2" ,
29+ "20, -4" ,
30+ "7, -1" ,
31+ "5, -1" ,
32+ })
33+ void
34+ testNegativeExponent (int x , int n ) {
35+ IllegalArgumentException exception = assertThrows (IllegalArgumentException .class , () -> ModuloPowerOfTwo .moduloPowerOfTwo (x , n ));
36+ assertEquals ("The exponent must be positive" , exception .getMessage ());
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments