Skip to content

Commit f3549e1

Browse files
HardvanChiefpatwal
authored andcommitted
Add HigherLowerPowerOfTwo algorithm (TheAlgorithms#5707)
1 parent 4fd7550 commit f3549e1

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java)
3131
* [GrayCodeConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/GrayCodeConversion.java)
3232
* [HammingDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HammingDistance.java)
33+
* [HigherLowerPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java)
3334
* [HighestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java)
3435
* [IndexOfRightMostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java)
3536
* [IsEven](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsEven.java)
@@ -658,6 +659,7 @@
658659
* [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java)
659660
* [GrayCodeConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/GrayCodeConversionTest.java)
660661
* [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HammingDistanceTest.java)
662+
* [HigherLowerPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java)
661663
* [HighestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java)
662664
* [IndexOfRightMostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBitTest.java)
663665
* [IsEvenTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsEvenTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* HigherLowerPowerOfTwo class has two methods to find the next higher and lower power of two.
5+
* <p>
6+
* nextHigherPowerOfTwo method finds the next higher power of two.
7+
* nextLowerPowerOfTwo method finds the next lower power of two.
8+
* Both methods take an integer as input and return the next higher or lower power of two.
9+
* If the input is less than 1, the next higher power of two is 1.
10+
* If the input is less than or equal to 1, the next lower power of two is 0.
11+
* nextHigherPowerOfTwo method uses bitwise operations to find the next higher power of two.
12+
* nextLowerPowerOfTwo method uses Integer.highestOneBit method to find the next lower power of two.
13+
* The time complexity of both methods is O(1).
14+
* The space complexity of both methods is O(1).
15+
* </p>
16+
*
17+
* @author Hardvan
18+
*/
19+
public final class HigherLowerPowerOfTwo {
20+
private HigherLowerPowerOfTwo() {
21+
}
22+
23+
/**
24+
* Finds the next higher power of two.
25+
*
26+
* @param x The given number.
27+
* @return The next higher power of two.
28+
*/
29+
public static int nextHigherPowerOfTwo(int x) {
30+
if (x < 1) {
31+
return 1;
32+
}
33+
x--;
34+
x |= x >> 1;
35+
x |= x >> 2;
36+
x |= x >> 4;
37+
x |= x >> 8;
38+
x |= x >> 16;
39+
return x + 1;
40+
}
41+
42+
/**
43+
* Finds the next lower power of two.
44+
*
45+
* @param x The given number.
46+
* @return The next lower power of two.
47+
*/
48+
public static int nextLowerPowerOfTwo(int x) {
49+
if (x < 1) {
50+
return 0;
51+
}
52+
return Integer.highestOneBit(x);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class HigherLowerPowerOfTwoTest {
8+
9+
@Test
10+
public void testNextHigherPowerOfTwo() {
11+
assertEquals(32, HigherLowerPowerOfTwo.nextHigherPowerOfTwo(19)); // next higher power of two is 32
12+
assertEquals(1, HigherLowerPowerOfTwo.nextHigherPowerOfTwo(1)); // next higher power of two is 1
13+
assertEquals(16, HigherLowerPowerOfTwo.nextHigherPowerOfTwo(15)); // next higher power of two is 16
14+
assertEquals(8, HigherLowerPowerOfTwo.nextHigherPowerOfTwo(8)); // next higher power of two is 8
15+
assertEquals(16, HigherLowerPowerOfTwo.nextHigherPowerOfTwo(9)); // next higher power of two is 16
16+
}
17+
18+
@Test
19+
public void testNextLowerPowerOfTwo() {
20+
assertEquals(16, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(19)); // next lower power of two is 16
21+
assertEquals(1, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(1)); // next lower power of two is 1
22+
assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(9)); // next lower power of two is 8
23+
assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(15)); // next lower power of two is 8
24+
assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(8)); // next lower power of two is 8
25+
}
26+
}

0 commit comments

Comments
 (0)