Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f280044

Browse files
author
Ram swaroop
committed
next lower power of 2: done
1 parent 779a0f7 commit f280044

File tree

1 file changed

+62
-25
lines changed

1 file changed

+62
-25
lines changed

src/me/ramswaroop/bits/NextPowerOf2.java

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
*/
1010
public class NextPowerOf2 {
1111

12-
1312
/**
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/>
1417
* Left shifts 1 number of times equal to the
1518
* leftmost set bit position in n.
1619
*
1720
* @param n
1821
* @return
1922
*/
20-
public static long nextPowerOf2(long n) {
23+
public static long nextHigherPowerOf2(long n) {
2124
// if n is already a power of 2 then return n
2225
if (n != 0 && ((n & (n - 1)) == 0)) return n;
2326

@@ -29,13 +32,17 @@ public static long nextPowerOf2(long n) {
2932
}
3033

3134
/**
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/>
3239
* Finds the leftmost set bit position and
3340
* left shifts 1 that many times.
3441
*
3542
* @param n
3643
* @return
3744
*/
38-
public static long nextPowerOf2_V2(long n) {
45+
public static long nextHigherPowerOf2_V2(long n) {
3946
// if n is already a power of 2 then return n
4047
if (n != 0 && ((n & (n - 1)) == 0)) return n;
4148

@@ -50,13 +57,17 @@ public static long nextPowerOf2_V2(long n) {
5057
}
5158

5259
/**
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/>
5364
* Finds the leftmost set bit position and
5465
* left shifts 1 that many times.
5566
*
5667
* @param n
5768
* @return
5869
*/
59-
public static long nextPowerOf2_V1(long n) {
70+
public static long nextHigherPowerOf2_V1(long n) {
6071
if (PowerOf2.isPowerOf2(n)) {
6172
return n;
6273
}
@@ -73,34 +84,60 @@ public static long nextPowerOf2_V1(long n) {
7384
return 1 << c;
7485
}
7586

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+
76103
public static void main(String a[]) {
77104

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));
85122

86123
System.out.println("=================");
87124

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));
95132

96133
System.out.println("=================");
97134

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));
105142
}
106143
}

0 commit comments

Comments
 (0)