forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextPowerOf2.java
143 lines (124 loc) · 3.93 KB
/
NextPowerOf2.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package me.ramswaroop.bits;
/**
* Created by IntelliJ IDEA.
*
* @author: ramswaroop
* @date: 6/2/15
* @time: 5:08 PM
*/
public class NextPowerOf2 {
/**
* Returns a power of 2 number which is larger
* than {@param n} but if {@param n} is already
* a power of 2 then it simply returns it.
* <p/>
* Left shifts 1 number of times equal to the
* leftmost set bit position in n.
*
* @param n
* @return
*/
public static long nextHigherPowerOf2(long n) {
// if n is already a power of 2 then return n
if (n != 0 && ((n & (n - 1)) == 0)) return n;
long p = 1;
while (p < n) {
p <<= 1;
}
return p;
}
/**
* Returns a power of 2 number which is larger
* than {@param n} but if {@param n} is already
* a power of 2 then it simply returns it.
* <p/>
* Finds the leftmost set bit position and
* left shifts 1 that many times.
*
* @param n
* @return
*/
public static long nextHigherPowerOf2_V2(long n) {
// if n is already a power of 2 then return n
if (n != 0 && ((n & (n - 1)) == 0)) return n;
long c = 0;
// finds the position of the leftmost set bit
while (n > 0) {
n >>= 1;
c++;
}
return 1 << c;
}
/**
* Returns a power of 2 number which is larger
* than {@param n} but if {@param n} is already
* a power of 2 then it simply returns it.
* <p/>
* Finds the leftmost set bit position and
* left shifts 1 that many times.
*
* @param n
* @return
*/
public static long nextHigherPowerOf2_V1(long n) {
if (PowerOf2.isPowerOf2(n)) {
return n;
}
int c = 0;
// finds the position of leftmost set bit
for (int i = 1; i <= 64; i++) {
if ((n & 1) == 1) {
c = i;
}
n >>= 1;
}
return 1 << c;
}
/**
* Returns a power of 2 number which is smaller
* than {@param n}.
*
* @param n
* @return
*/
public static long nextLowerPowerOf2(long n) {
long p = 1;
while (p < n) {
p <<= 1;
}
return (n > 1) ? p >> 1 : n; // check for n = 0 or 1;
}
public static void main(String a[]) {
System.out.println(nextHigherPowerOf2(2));
System.out.println(nextHigherPowerOf2(3));
System.out.println(nextHigherPowerOf2(18));
System.out.println(nextHigherPowerOf2(6));
System.out.println(nextHigherPowerOf2(7));
System.out.println(nextHigherPowerOf2(1));
System.out.println(nextHigherPowerOf2(0));
System.out.println("=================");
System.out.println(nextHigherPowerOf2_V2(2));
System.out.println(nextHigherPowerOf2_V2(3));
System.out.println(nextHigherPowerOf2_V2(18));
System.out.println(nextHigherPowerOf2_V2(6));
System.out.println(nextHigherPowerOf2_V2(7));
System.out.println(nextHigherPowerOf2_V2(1));
System.out.println(nextHigherPowerOf2_V2(0));
System.out.println("=================");
System.out.println(nextHigherPowerOf2_V1(2));
System.out.println(nextHigherPowerOf2_V1(3));
System.out.println(nextHigherPowerOf2_V1(18));
System.out.println(nextHigherPowerOf2_V1(6));
System.out.println(nextHigherPowerOf2_V1(7));
System.out.println(nextHigherPowerOf2_V1(1));
System.out.println(nextHigherPowerOf2_V1(0));
System.out.println("=================");
System.out.println(nextLowerPowerOf2(2));
System.out.println(nextLowerPowerOf2(3));
System.out.println(nextLowerPowerOf2(18));
System.out.println(nextLowerPowerOf2(6));
System.out.println(nextLowerPowerOf2(7));
System.out.println(nextLowerPowerOf2(1));
System.out.println(nextLowerPowerOf2(0));
}
}