R
ravi
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
[No loops allowed]
No, and why are you giving orders?ravi said:Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
ravi said:Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
ravi said:Give a one-line C expression to test whether a number is a power
of 2. [No loops allowed]
ravi said:Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
No, and why are you giving orders?
ravi said:Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
ravi said:Give a one-line C expression to test whether a number is a power of
2.
[No loops allowed]
static inline int
is_power_of_2 (int x)
{
return ((x & (x - 1)) == 0) ;
}
ravi said:ravi said:Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
No, and why are you giving orders?
Beacuse I am your father
What if x <= 0 (in which case x cannot be a power of two)?ravi said:Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
static inline int
is_power_of_2 (int x)
{
return ((x & (x - 1)) == 0) ;
ravi said:ravi said:Give a one-line C expression
to test whether a number is a power of 2.
[No loops allowed]
No, and why are you giving orders?
Beacuse I am your father
Richard said:Erik de Castro Lopo said:
Having proved willing to do homework for free, you and Chuck might want
to brace yourselves for the rush.
ravi said:No, and why are you giving orders?ravi said:Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
Beacuse I am your father
pete said:ravi wrote:
Give a one-line C expression
to test whether a number is a power of 2.
[No loops allowed]
No, and why are you giving orders?
Beacuse I am your father
http://www.dockingbay101.com/sounds/anakin/ep5/father.wav
ravi said:ravi wrote:
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
No, and why are you giving orders?
Beacuse I am your father
That explains my congenital idiocy.
Flash said:Eric Sosman wrote, On 08/07/07 13:27:ravi said:ravi wrote:
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
No, and why are you giving orders?
Beacuse I am your father
That explains my congenital idiocy.
Probably. Had you enough sense you would have instantly known the answer
was:
int powerof2( double num ) { return 1; }
After all, the OP did not specify integer power of 2, or even real power
of 2, and I think if you allow complex powers, all numbers will qualify.
Flash said:Eric Sosman wrote, On 08/07/07 13:27:Probably. Had you enough sense you would have instantly known the answerravi wrote:
ravi wrote:
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
No, and why are you giving orders?
Beacuse I am your father
That explains my congenital idiocy.
was:
int powerof2( double num ) { return 1; }
After all, the OP did not specify integer power of 2, or even real power
of 2, and I think if you allow complex powers, all numbers will qualify.
What about zeroes, infinities, and NaNs? I would think it should return 0
for those.
I like my version betterIn all seriousness, this is not such
a simple thing, so I doubt it was homework. If he hadn't used such
rude terminology I would be virtually sure it is not.
Christopher said:Given the number of ridiculous questions that are routinely asked here
that clearly *are* homework, I'm surprised you find it unlikely that
the above question is not homework. Why else would there be
constraints such as "one line" or "no loops allowed" (snipped from
above context)? I personally find it highly unlikely that anyone
asking the question seriously would phrase it so tersely.
ravi said:Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]static inline int
is_power_of_2 (int x)
{
return ((x & (x - 1)) == 0) ;
What if x <= 0 (in which case x cannot be a power of two)?
Flash said:Eric Sosman wrote, On 08/07/07 13:27:ravi wrote:
ravi wrote:
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed][...] Had you enough sense you would have instantly known the answer
was:int powerof2( double num ) { return 1; }After all, the OP did not specify integer power of 2, or even real power
of 2, and I think if you allow complex powers, all numbers will qualify.
What about zeroes, infinities, and NaNs? I would think it should return 0
for those
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.