Using concept of "n&(n-1)" makes value = 0 if n is number with power of 2
#include <stdio.h>
int nextPwrOf2(int num)
{
do
{
num++;
}
while (( num & num-1) != 0);
return num;
}
int main()
{
int num=15;
int nextPwr;
nextPwr=nextPwrOf2(num);
printf(" %d \n",nextPwr);
return 0;
}
No comments:
Post a Comment