Power of 2
-
Crikey. I thought this was well known. Just do this:
if (x & (x-1))
{
// x is <edit>not</edit>a power of two
}
else
{
// x is <edit>not</edit> a power of two
}sorry, got the two cases round the wrong way... :-O
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-- modified at 6:04 Monday 6th March, 2006
Hi Ryan, Is there is quick way for now making x a power of 2 after you have decided that it is not? regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook
-
Modified version is still wrong - It will treat any integer as a power of 2. Steve
-
Modified version is still wrong - It will treat any integer as a power of 2. Steve
-
Hi All, Is there a fast way of checking to see if an
int
is a power of 2 (i.e. 2, 4, 8, 16, 1024)? I know the long way using a loop, but I was hoping for a simple way of doing this. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook#define ISPOWER2(x) (!((x)&((x)-1)))
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
Read this function, it could be another start point! This function that I wrote last year find the bigger long, greater than the input value, that is a power of 2. And it is quite fast!:)
unsigned long Next2Power(unsigned long x){ unsigned long y=1, x1=x; if(x==0) return 0; while(x1!=0){ x1>>=1; y<<=1; } y>>=1; if(y!=x) y<<=1; return y; }
Have a nice code day ;)
_Russell_ wrote:
This function that I wrote last year find the bigger long, greater than the input value, that is a power of 2.
Why not just use
log2()
?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
Hi Ryan, Is there is quick way for now making x a power of 2 after you have decided that it is not? regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook
RichardS wrote:
Is there is quick way for now making x a power of 2 after you have decided that it is not?
Not that I know of. I've never had to do this. Would you round up or down?
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
RichardS wrote:
Is there is quick way for now making x a power of 2 after you have decided that it is not?
Not that I know of. I've never had to do this. Would you round up or down?
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Round up. So far I have a loop (for 32-bits) shifting 0x80000000 down until the & with x is successful, then shifting it back up by one. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook
-
_Russell_ wrote:
This function that I wrote last year find the bigger long, greater than the input value, that is a power of 2.
Why not just use
log2()
?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
Because my function is faster (I think) than log2(). But if I'm wrong let me know how have a faster function. And why. thanks
Have a nice code day ;)
_Russell_ wrote:
...let me know how have a faster function.
This one benchmarks faster than the one you've shown:
unsigned long Next2Power(unsigned long x)
{
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;return x;
}
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
Hi Ryan, Is there is quick way for now making x a power of 2 after you have decided that it is not? regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook
#define CK_ISPOW2(n) ( ((n)&((n)-1)) == 0 )
ullong FkGrainDn2( ullong S, ullong G )
{
// if( !CK_ISPOW2(G) ) return( FkGrainDnN(S, G) );
S = S & ~(G-1);
return(S);
}ullong FkGrainUp2( ullong S, ullong G )
{
// if( !CK_ISPOW2(G) ) return( FkGrainUpN(S, G) );
G -= 1;
S = (S+G) & ~G;
return(S);
}where: S is value to round G is a power of 2 ...cmk Save the whales - collect the whole set