Skip to content

Commit f35e6b9

Browse files
committed
Add Implementation of Next Power of 2
1 parent e5bf4b3 commit f35e6b9

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Math/NextPow2/nextpow2.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
3+
/**
4+
* nextpow2 - Implementation of Next Power of 2
5+
*/
6+
int nextpow2(int n)
7+
{
8+
int c = 0;
9+
if (n < 1)
10+
return 1;
11+
--n;
12+
while (n)
13+
{
14+
++c;
15+
n >>= 1;
16+
}
17+
return 1 << c;
18+
}
19+
20+
int main()
21+
{
22+
int n;
23+
std::cin >> n;
24+
std::cout << nextpow2(n) << '\n';
25+
return 0;
26+
}

0 commit comments

Comments
 (0)