to test whether a number is a power of 2

R

ravi

Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
 
C

CBFalconer

ravi said:
Give a one-line C expression to test whether a number is a power
of 2. [No loops allowed]

For unsigned integers:

if (!((n - 1) & n)) puts("n is power of 2");
 
E

Erik de Castro Lopo

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) ;
}


--
 
R

Richard Heathfield

Erik de Castro Lopo said:
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) ;
}

Having proved willing to do homework for free, you and Chuck might want
to brace yourselves for the rush.
 
S

santosh

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

Most participants of this group an, (not unreasonable), expectation of
people who post homework questions to have at least made an honest
attempt at the problem. This group hosts, perhaps, the best minds in C
you're likely to meet, but you, (as a new participant and a learner),
need to show willingness to put in some effort on your part, (like
trying to remain civil), to get the best response and help. At least
that been my experience in my participation in this group.
 
A

Army1987

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)?
 
C

CBFalconer

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.

I like my version better :) In 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.
 
F

Flash Gordon

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.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

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.

What about zeroes, infinities, and NaNs? I would think it should return 0
for those.
 
F

Flash Gordon

Harald van Dijk wrote, On 08/07/07 23:37:
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]
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.

What about zeroes, infinities, and NaNs? I would think it should return 0
for those.

Zero is easy to deal with, infinities could be argued as being powers of
2, and change the input parameter to an integer type to get rid of the
problem of NaNs :) Alternatively, in C99 you can use isnan and isinf to
solve the problem.

int powerof2(double num) {return !(isnan(num) || isinf(num) || num==0)}

There is, of course, the question of whether you want to count numbers
that are almost zero as zero because of rounding, but I'll leave dealing
with that as an exercise to anyone wanting that functionality.
 
C

Christopher Benson-Manica

[OP wrote:]
I like my version better :) In 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.

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.
 
C

CBFalconer

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.

You have a definite point, and are probably right.
 
W

websnarf

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)?

Oh for crying out loud! The other answers had the OP perfectly set up
with the wrong answer (brilliant coordination, BTW, guys -- all of you
getting it simultaneously wrong in exactly the same way). Now the OP
can figure it out completely correctly without any research himself.
Good job.
 
N

Nick Keighley

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

ah, but the spec said test a *number* and I don't think infinity is a
number and as for NaN...

So it should indicate some sort of domain error or assert on a debug
build.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top