Write an efficient function to round a number to next highest power of 2
[code:1]v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++;[/code:1]
This is a much faster way than using all the log functions from the math library
The fastest technique I could think of was using math.h library:
#include #include
void main() { float number; long result; //this is taken with purpose
scanf("%f",&number);
result=log(number)/log(2);
printf("%ld",(long)pow(2,result+1)); }
Rounding a number to next highest power of 2
[code:1]v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;[/code:1]
This is a much faster way than using all the log functions from the math library
Rounding a number to next highest power of 2
The fastest technique I could think of was using math.h library:
#include
#include
void main()
{
float number;
long result; //this is taken with purpose
scanf("%f",&number);
result=log(number)/log(2);
printf("%ld",(long)pow(2,result+1));
}