Skip to content

Commit 5d0fb1e

Browse files
committed
feat: Add HigherLowerPowerOfTwo new algorithm with Junit tests
1 parent 90d20b3 commit 5d0fb1e

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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) return 1;
31+
x--;
32+
x |= x >> 1;
33+
x |= x >> 2;
34+
x |= x >> 4;
35+
x |= x >> 8;
36+
x |= x >> 16;
37+
return x + 1;
38+
}
39+
40+
/**
41+
* Finds the next lower power of two.
42+
*
43+
* @param x The given number.
44+
* @return The next lower power of two.
45+
*/
46+
public static int nextLowerPowerOfTwo(int x) {
47+
if (x < 1) return 0;
48+
return Integer.highestOneBit(x);
49+
}
50+
}
Lines changed: 26 additions & 0 deletions
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)