|
-
May 26th, 2007, 01:31 PM
#1
Will you solve this tricky question?
how would you decide a number is some power of 2 in c? no loops are allowed and this should be done in one statement.
Last edited by RAJJUMOTE; May 26th, 2007 at 01:34 PM.
-
May 26th, 2007, 02:00 PM
#2
Re: Will you solve this tricky question?
Code:
bool IsPowerOfTwo (int number)
{
return number ? ((number & -number)==number):false;
}
Laitinen
-
May 26th, 2007, 02:41 PM
#3
Re: Will you solve this tricky question?
double SomeNumber, SomeExponent;
SomeNumber = 2**SomeExponent;
log(SomeNumber) = SomeExponent * log(2);
SomeExponent = log(SomeNumber) / log(2);
is SomeExponent an integer?
in theory:
if SomeExponent - floor(SomeExponent) = 0;
in practice:
double epsilon;
epsilon = .00001;
if abs( SomeExponent - floor(SomeExponent) ) < epsilon;
-
May 26th, 2007, 03:57 PM
#4
Re: Will you solve this tricky question?
Pretty cool solution Latinen.
-
May 26th, 2007, 05:09 PM
#5
Re: Will you solve this tricky question?
Folks, we should be determining that this is not a homework question, or that the OP has at least attempted it, before answering. The form of the OP certainly suggests that it is.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
May 26th, 2007, 05:13 PM
#6
Re: Will you solve this tricky question?
 Originally Posted by S_M_A
Pretty cool solution Latinen.
For integers, there's an even simpler solution.
(ETA: I suppose I should say conceptually simpler...)
Last edited by Graham; May 27th, 2007 at 10:52 AM.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
May 27th, 2007, 09:29 PM
#7
Re: Will you solve this tricky question?
let me remove my codes.
Last edited by sandodo; May 28th, 2007 at 12:23 AM.
-
May 27th, 2007, 10:40 PM
#8
Re: Will you solve this tricky question?
 Originally Posted by RAJJUMOTE
how would you decide a number is some power of 2 in c? no loops are allowed and this should be done in one statement.
Note that it's always the person with only 1 or 2 posts that asks these kind of questions.
Definitely a homework question, since the OP has not appeared since he now has something to take back to the teacher. We should be much better sniffing out "do my homework" posts, and not provide answers.
Regards,
Paul McKenzie
-
May 27th, 2007, 11:18 PM
#9
Re: Will you solve this tricky question?
 Originally Posted by Paul McKenzie
Note that it's always the person with only 1 or 2 posts that asks these kind of questions.
Definitely a homework question, since the OP has not appeared since he now has something to take back to the teacher. We should be much better sniffing out "do my homework" posts, and not provide answers.
Regards,
Paul McKenzie
See signature below...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|