#1




Quick test whether a number is a power of twoI was wondering, is there an efficient (speed, memory?) test for whether or not a number is a power of 2 (or anything else).
I'm writing a programme where it would be handy to be able to make that kind of check. An implementation I've used before was something like: CPP / C++ / C Code:
That doesn't look very efficient to me though. Is there a faster way? 

#2




Re: Quick test whether a number is a power of twoQuote:
If you use float (or double ) it is always suspectible to rounding errors. Therefore in the end when you compare ( y != 1) you may unexpectedly get "wrong" answer because floating point numbers are not mathematically exact but instead each calculation increases the cumulative rounding error. the simplest I could imagine was: CPP / C++ / C Code:
If someone can do without even the whileloop that would be interesting to see. 
#3




Re: Quick test whether a number is a power of twoQuote:
CPP / C++ / C Code:
From The C Programming Language by Brian W. Kerhighan and Dennis M. Ritchie. First edition in 1978, second edition in 1988. Bottom line: If your system has 2's complement representation of integers (as is highly likely), this function returns a value of 1 if (and only if) the number is a power of 2. It only has logic operations on the number itself. (The only arithmetic operation is the part that increments the loop counter.) CPP / C++ / C Code:
Code:
Regards, Dave 
#4




Re: Quick test whether a number is a power of twoOne line test to determine if the number is power of 2 or not.
/************************************************** ***/ int main() { unsigned long int var = 2147483648; if ( var & (var1) ) printf ("%u is not power of 2\n", var); else printf ("%u is power of 2\n", var); return 0; } /************************************************** ***/ cheers, Sandeep 
#5




Re: Quick test whether a number is a power of twoQuote:
It works for everything but zero. It reports zero is a power of 2. Maybe CPP / C++ / C Code:
Still a oneliner; still pretty elegant. Makes you look at what happens when you apply bittwiddling to binary integers. Maybe a little insight, maybe a little fun. Thanks for that. Regards, Dave 
#6




Re: Quick test whether a number is a power of twoThanks very much guys. Those looks ideal.
A slight pity though, they only work for x = 2^k. It would be nice to have something that does x = 3^k, etc. Is the best way to do that just the way seprich and I used? 
«
Previous Thread

Next Thread
»
Thread Tools  Search this Thread 
Rate This Thread  



Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Check a number whether it is a sqrt of another number.  dendelion  C Programming Language  10  07Aug2006 01:48 
Converting a number amount to text  Godzilla  C++ Forum  5  31Mar2006 12:38 
Need Help with my Cards Program (C++)  krisopotamus  C++ Forum  2  06Oct2005 16:48 
fltk2.0 cvs  Plumb  FLTK Forum  20  13Nov2004 08:10 
Anyone can write a program code for this???  chriskan76  C Programming Language  1  19Oct2004 20:25 
Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The
All times are GMT 6. The time now is 20:31.