File tree 3 files changed +54
-0
lines changed
main/java/com/thealgorithms/bitmanipulation
test/java/com/thealgorithms/bitmanipulation
3 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 41
41
* [ IsPowerTwo] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java )
42
42
* [ LowestSetBit] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java )
43
43
* [ 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 )
44
45
* [ NonRepeatingNumberFinder] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java )
45
46
* [ NumberAppearingOddTimes] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java )
46
47
* [ NumbersDifferentSigns] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java )
744
745
* [ IsPowerTwoTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java )
745
746
* [ LowestSetBitTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java )
746
747
* [ 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 )
747
749
* [ NonRepeatingNumberFinderTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java )
748
750
* [ NumberAppearingOddTimesTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java )
749
751
* [ 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