-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathHigherLowerPowerOfTwo.java
54 lines (51 loc) · 1.62 KB
/
HigherLowerPowerOfTwo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.thealgorithms.bitmanipulation;
/**
* HigherLowerPowerOfTwo class has two methods to find the next higher and lower power of two.
* <p>
* nextHigherPowerOfTwo method finds the next higher power of two.
* nextLowerPowerOfTwo method finds the next lower power of two.
* Both methods take an integer as input and return the next higher or lower power of two.
* If the input is less than 1, the next higher power of two is 1.
* If the input is less than or equal to 1, the next lower power of two is 0.
* nextHigherPowerOfTwo method uses bitwise operations to find the next higher power of two.
* nextLowerPowerOfTwo method uses Integer.highestOneBit method to find the next lower power of two.
* The time complexity of both methods is O(1).
* The space complexity of both methods is O(1).
* </p>
*
* @author Hardvan
*/
public final class HigherLowerPowerOfTwo {
private HigherLowerPowerOfTwo() {
}
/**
* Finds the next higher power of two.
*
* @param x The given number.
* @return The next higher power of two.
*/
public static int nextHigherPowerOfTwo(int x) {
if (x < 1) {
return 1;
}
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x + 1;
}
/**
* Finds the next lower power of two.
*
* @param x The given number.
* @return The next lower power of two.
*/
public static int nextLowerPowerOfTwo(int x) {
if (x < 1) {
return 0;
}
return Integer.highestOneBit(x);
}
}