9
9
*/
10
10
public class NextPowerOf2 {
11
11
12
-
13
12
/**
13
+ * Returns a power of 2 number which is larger
14
+ * than {@param n} but if {@param n} is already
15
+ * a power of 2 then it simply returns it.
16
+ * <p/>
14
17
* Left shifts 1 number of times equal to the
15
18
* leftmost set bit position in n.
16
19
*
17
20
* @param n
18
21
* @return
19
22
*/
20
- public static long nextPowerOf2 (long n ) {
23
+ public static long nextHigherPowerOf2 (long n ) {
21
24
// if n is already a power of 2 then return n
22
25
if (n != 0 && ((n & (n - 1 )) == 0 )) return n ;
23
26
@@ -29,13 +32,17 @@ public static long nextPowerOf2(long n) {
29
32
}
30
33
31
34
/**
35
+ * Returns a power of 2 number which is larger
36
+ * than {@param n} but if {@param n} is already
37
+ * a power of 2 then it simply returns it.
38
+ * <p/>
32
39
* Finds the leftmost set bit position and
33
40
* left shifts 1 that many times.
34
41
*
35
42
* @param n
36
43
* @return
37
44
*/
38
- public static long nextPowerOf2_V2 (long n ) {
45
+ public static long nextHigherPowerOf2_V2 (long n ) {
39
46
// if n is already a power of 2 then return n
40
47
if (n != 0 && ((n & (n - 1 )) == 0 )) return n ;
41
48
@@ -50,13 +57,17 @@ public static long nextPowerOf2_V2(long n) {
50
57
}
51
58
52
59
/**
60
+ * Returns a power of 2 number which is larger
61
+ * than {@param n} but if {@param n} is already
62
+ * a power of 2 then it simply returns it.
63
+ * <p/>
53
64
* Finds the leftmost set bit position and
54
65
* left shifts 1 that many times.
55
66
*
56
67
* @param n
57
68
* @return
58
69
*/
59
- public static long nextPowerOf2_V1 (long n ) {
70
+ public static long nextHigherPowerOf2_V1 (long n ) {
60
71
if (PowerOf2 .isPowerOf2 (n )) {
61
72
return n ;
62
73
}
@@ -73,34 +84,60 @@ public static long nextPowerOf2_V1(long n) {
73
84
return 1 << c ;
74
85
}
75
86
87
+
88
+ /**
89
+ * Returns a power of 2 number which is smaller
90
+ * than {@param n}.
91
+ *
92
+ * @param n
93
+ * @return
94
+ */
95
+ public static long nextLowerPowerOf2 (long n ) {
96
+ long p = 1 ;
97
+ while (p < n ) {
98
+ p <<= 1 ;
99
+ }
100
+ return (n > 1 ) ? p >> 1 : n ; // check for n = 0 or 1;
101
+ }
102
+
76
103
public static void main (String a []) {
77
104
78
- System .out .println (nextPowerOf2 (2 ));
79
- System .out .println (nextPowerOf2 (3 ));
80
- System .out .println (nextPowerOf2 (18 ));
81
- System .out .println (nextPowerOf2 (6 ));
82
- System .out .println (nextPowerOf2 (7 ));
83
- System .out .println (nextPowerOf2 (1 ));
84
- System .out .println (nextPowerOf2 (0 ));
105
+ System .out .println (nextHigherPowerOf2 (2 ));
106
+ System .out .println (nextHigherPowerOf2 (3 ));
107
+ System .out .println (nextHigherPowerOf2 (18 ));
108
+ System .out .println (nextHigherPowerOf2 (6 ));
109
+ System .out .println (nextHigherPowerOf2 (7 ));
110
+ System .out .println (nextHigherPowerOf2 (1 ));
111
+ System .out .println (nextHigherPowerOf2 (0 ));
112
+
113
+ System .out .println ("=================" );
114
+
115
+ System .out .println (nextHigherPowerOf2_V2 (2 ));
116
+ System .out .println (nextHigherPowerOf2_V2 (3 ));
117
+ System .out .println (nextHigherPowerOf2_V2 (18 ));
118
+ System .out .println (nextHigherPowerOf2_V2 (6 ));
119
+ System .out .println (nextHigherPowerOf2_V2 (7 ));
120
+ System .out .println (nextHigherPowerOf2_V2 (1 ));
121
+ System .out .println (nextHigherPowerOf2_V2 (0 ));
85
122
86
123
System .out .println ("=================" );
87
124
88
- System .out .println (nextPowerOf2_V2 (2 ));
89
- System .out .println (nextPowerOf2_V2 (3 ));
90
- System .out .println (nextPowerOf2_V2 (18 ));
91
- System .out .println (nextPowerOf2_V2 (6 ));
92
- System .out .println (nextPowerOf2_V2 (7 ));
93
- System .out .println (nextPowerOf2_V2 (1 ));
94
- System .out .println (nextPowerOf2_V2 (0 ));
125
+ System .out .println (nextHigherPowerOf2_V1 (2 ));
126
+ System .out .println (nextHigherPowerOf2_V1 (3 ));
127
+ System .out .println (nextHigherPowerOf2_V1 (18 ));
128
+ System .out .println (nextHigherPowerOf2_V1 (6 ));
129
+ System .out .println (nextHigherPowerOf2_V1 (7 ));
130
+ System .out .println (nextHigherPowerOf2_V1 (1 ));
131
+ System .out .println (nextHigherPowerOf2_V1 (0 ));
95
132
96
133
System .out .println ("=================" );
97
134
98
- System .out .println (nextPowerOf2_V1 (2 ));
99
- System .out .println (nextPowerOf2_V1 (3 ));
100
- System .out .println (nextPowerOf2_V1 (18 ));
101
- System .out .println (nextPowerOf2_V1 (6 ));
102
- System .out .println (nextPowerOf2_V1 (7 ));
103
- System .out .println (nextPowerOf2_V1 (1 ));
104
- System .out .println (nextPowerOf2_V1 (0 ));
135
+ System .out .println (nextLowerPowerOf2 (2 ));
136
+ System .out .println (nextLowerPowerOf2 (3 ));
137
+ System .out .println (nextLowerPowerOf2 (18 ));
138
+ System .out .println (nextLowerPowerOf2 (6 ));
139
+ System .out .println (nextLowerPowerOf2 (7 ));
140
+ System .out .println (nextLowerPowerOf2 (1 ));
141
+ System .out .println (nextLowerPowerOf2 (0 ));
105
142
}
106
143
}
0 commit comments