for example, n=5 so p will be equal to 8 because 8/2=4, 4/2=2

it can’t be 6 because 6/2=3 and i need the smallest power to be 2

I don’t know anything about how i’d go along with this program

You can use bitwise shift operators to solve your problem . Start with number=1 .

Compare with n everytime until the number or acc. to you p , becomes greater than n . Until then multiply the number by 2 using bitshift . For example.

number=number<<1;

int expo= ceil(log(n)/log(2.0));

unsigned long long int ans= pow(2,expo);// Works for 0<n<=2^64

2 simple lines of code

We are basically using fact that log(n) to base 2, when rounded up, give an integer K, which is the power to which 2 must be raised.

Basic, Easy to understand approach for this…

int n = given no

int output = 2;

while(output < n){

output = output*2;

}

output variable holds the required answer.

Otherwise you can go with the above technique mentioned by vijju123

Please Upvote and Accept if you find this helpful…

This simple code should do the trick considering the fact that log2(n) give the number (float) to which it 2 should be powered for giving the number n i.e 2^(log2(n)) = n.

```
//You should include math.h
int answer = (int)pow(2,(int)log2(n))
```

Hope this will help. Upvote in case

It’s a standard problem. This link would give you a list of approaches: http://www.geeksforgeeks.org/smallest-power-of-2-greater-than-or-equal-to-n/

Thanks a lot! This was nice and simple.

Nice and Easy does the trick…