File tree Expand file tree Collapse file tree 3 files changed +54
-0
lines changed
main/java/com/thealgorithms/bitmanipulation
test/java/com/thealgorithms/bitmanipulation Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 4141 * [ IsPowerTwo] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java )
4242 * [ LowestSetBit] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java )
4343 * [ ModuloPowerOfTwo] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwo.java )
44+ * [ NextHigherSameBitCount] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NextHigherSameBitCount.java )
4445 * [ NonRepeatingNumberFinder] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java )
4546 * [ NumberAppearingOddTimes] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java )
4647 * [ NumbersDifferentSigns] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java )
744745 * [ IsPowerTwoTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java )
745746 * [ LowestSetBitTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java )
746747 * [ ModuloPowerOfTwoTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java )
748+ * [ NextHigherSameBitCountTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NextHigherSameBitCountTest.java )
747749 * [ NonRepeatingNumberFinderTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java )
748750 * [ NumberAppearingOddTimesTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java )
749751 * [ 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 find the next higher number
5+ * with the same number of set bits as the given number.
6+ *
7+ * @author Hardvan
8+ */
9+ public final class NextHigherSameBitCount {
10+ private NextHigherSameBitCount () {
11+ }
12+
13+ /**
14+ * Finds the next higher integer with the same number of set bits.
15+ * Steps:
16+ * 1. Find {@code c}, the rightmost set bit of {@code n}.
17+ * 2. Find {@code r}, the rightmost set bit of {@code n + c}.
18+ * 3. Swap the bits of {@code r} and {@code n} to the right of {@code c}.
19+ * 4. Shift the bits of {@code r} and {@code n} to the right of {@code c} to the rightmost.
20+ * 5. Combine the results of steps 3 and 4.
21+ *
22+ * @param n the input number
23+ * @return the next higher integer with the same set bit count
24+ */
25+ public static int nextHigherSameBitCount (int n ) {
26+ int c = n & -n ;
27+ int r = n + c ;
28+ return (((r ^ n ) >> 2 ) / c ) | r ;
29+ }
30+ }
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+
5+ import org .junit .jupiter .params .ParameterizedTest ;
6+ import org .junit .jupiter .params .provider .CsvSource ;
7+
8+ class NextHigherSameBitCountTest {
9+
10+ @ ParameterizedTest
11+ @ CsvSource ({
12+ "5, 6" , // 101 -> 110
13+ "7, 11" , // 0111 -> 1011
14+ "3, 5" , // 011 -> 101
15+ "12, 17" , // 001100 -> 010001
16+ "15, 23" // 01111 -> 10111
17+ })
18+ void
19+ testNextHigherSameBitCount (int input , int expected ) {
20+ assertEquals (expected , NextHigherSameBitCount .nextHigherSameBitCount (input ));
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments