Skip to content

Commit 94daff0

Browse files
authored
Add NextHigherSameBitCount algorithm (#5865)
1 parent 9e1dd86 commit 94daff0

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
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)
@@ -744,6 +745,7 @@
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)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)