|
Hi,
you could use ::log to check if its a power of 2
math:
y = 2^x -> x = log(y)/log(2)
so you can use:
int y = 1024;
if(y == (int)(::pow(2.0, ::floor(::log((double)y)/::log(2.0)))))
{
}
else
{
}
but his is heavy processing,
so it mith be slower that the for loop
codito ergo sum
|
|
|
|
|
Define "fast".
If you're using 32-bit int's, there are only 32 possibilities so you can just check entries in a lookup table, making sure you do the most likely candidates first.
BTW, all these suggestions to use sqrt() and log() are nuts - these functions are insanely slow!
The two most common elements in the universe are Hydrogen and stupidity. - Harlan Ellison
Awasu 2.2 [^]: A free RSS/Atom feed reader with support for Code Project.
|
|
|
|
|
Taka Muraoka wrote: BTW, all these suggestions to use sqrt() and log() are nuts - these functions are insanely slow!
Agreed!! It is quite simple. A single decrement and bitwise AND will do the job nicely
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"
|
|
|
|
|
Crikey. I thought this was well known. Just do this:
if (x & (x-1))
{
}
else
{
}
sorry, got the two cases round the wrong way...
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
|
|
|
|
|
This is by far the best suggestion so far - I'd wager it's impossible to beat this technique.
Steve
|
|
|
|
|
And yet it got the lowest votes of all of them. Funny, isn't it
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"
|
|
|
|
|
I gave you a 5 - I would have left it at that but then I saw the low score and felt compelled to reply.
Steve
|
|
|
|
|
Thanks I don't really care. I assume it was because of my first sentence. I was just surprised that five people answered, but not one of them knew about this. I genuinely believed it was well known!
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"
|
|
|
|
|
Surely using the word "crikey" demands higher rather than lower scores!
Steve
|
|
|
|
|
!
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"
|
|
|
|
|
Great work!
You've implemented Russel's idea!
Nibu thomas
Software Developer
|
|
|
|
|
Actually, I've been using this for years doing embedded programming. Every clock cycle is precious
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"
|
|
|
|
|
Yeah, well said.
Nibu thomas
Software Developer
|
|
|
|
|
Hi Ryan,
Thanks for the help. The code is great and the application is for an embedded -> PC interface.
cheers
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
|
|
|
|
|
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"
|
|
|
|
|
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
|
|
|
|
|
#define CK_ISPOW2(n) ( ((n)&((n)-1)) == 0 )
ullong FkGrainDn2( ullong S, ullong G )
{
S = S & ~(G-1);
return(S);
}
ullong FkGrainUp2( ullong S, ullong 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
|
|
|
|
|
#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
|
|
|
|
|
Hi all
could you please anyone tell me what is the equivalent of
CSimpleStringT::GetBuffer in C#
Thanks and Regards
Madhu
|
|
|
|
|
When using the "StretchBlt" copy the bitmap to the target DlgItem.
It success to draw it on the screen, but when i using "ShowWindow" and "SetWindowPos".
The image on the dlgitem will lost
have any method to keep the image in the screen when it hiden or resize ?
|
|
|
|
|
Do the StretchBlt (and any other drawing) in a WM_PAINT handler.
Steve
|
|
|
|
|
i want to handle the event WM_DROPFILES and i am using MFC dialog base application but the classwizar of this dialog does not show this event so how to handle this event if anyone know please answer me ASAP.
Bankey Khandelwal
Software Engineer
|
|
|
|
|
I gave you a link in the previous post. It has got all sorts of samples to demonstrate this to you. Please go through them.
Go through the article before you run them.
Nibu thomas
Software Developer
|
|
|
|
|
i am trying to made a class which have to drive from 'COleDropTarget'
but on my site installed visual studio does not show this class as base so please tell what is the reason.
Bankey Khandelwal
Software Engineer
|
|
|
|