File tree 3 files changed +82
-0
lines changed
main/java/com/thealgorithms/bitmanipulation
test/java/com/thealgorithms/bitmanipulation
3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change 30
30
* [ CountSetBits] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java )
31
31
* [ GrayCodeConversion] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/GrayCodeConversion.java )
32
32
* [ 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 )
33
34
* [ HighestSetBit] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java )
34
35
* [ IndexOfRightMostSetBit] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java )
35
36
* [ IsEven] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsEven.java )
658
659
* [ CountSetBitsTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java )
659
660
* [ GrayCodeConversionTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/GrayCodeConversionTest.java )
660
661
* [ 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 )
661
663
* [ HighestSetBitTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java )
662
664
* [ IndexOfRightMostSetBitTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBitTest.java )
663
665
* [ IsEvenTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsEvenTest.java )
Original file line number Diff line number Diff line change
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 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 .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
+ }
You can’t perform that action at this time.
0 commit comments