to test whether a number is a power of 2

W

Walter Roberson

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

But the OP did specify that the expression be one-line. Even if we
were to restrict our use of "number" to "the numbers representable
in C", since C does not have any run-time typing (RTT), I don't think
it would be possible to write a one-line expression that would take
an arbitrary number (of -any- of the valid C numeric types) as parameter
and return any meaningful information about it. If we could at least
receive the number as a void* and a type indicator, we could unionize
and ?: the problem to death.
 
K

Kenneth Brody

Walter 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]
[...]
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.

But the OP did specify that the expression be one-line. Even if we
were to restrict our use of "number" to "the numbers representable
in C", since C does not have any run-time typing (RTT), I don't think
it would be possible to write a one-line expression that would take
an arbitrary number (of -any- of the valid C numeric types) as parameter
and return any meaningful information about it. If we could at least
receive the number as a void* and a type indicator, we could unionize
and ?: the problem to death.

Why not a macro?

#define POWEROF2(value) (1)

However, can zero be expressed as a power of 2? (I suppose raising 2
to the power of negative infinity may qualify, but I'm not certain.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
W

Walter Roberson

Flash Gordon said:
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.

Hey, I just realized that the specification required that the
expression -test- that the number was a power of 2, but the
specification didn't require that the expression evaluated to
anything in particular either way -- or even that the expression
got the test right in any or all cases.

For example, we commonly speak of "testing whether a number is prime"
and in a number of cases are satisfied if the test is looking for
"obvious" non-primes, something fast as a filter to keep us from
having to use slower but more complete algorithms.
 
F

Flash Gordon

Walter Roberson wrote, On 10/07/07 19:09:
Hey, I just realized that the specification required that the
expression -test- that the number was a power of 2,

See elsethread where I added some tests. Specifically for inf, NaN and 0.
> but the
specification didn't require that the expression evaluated to
anything in particular either way -- or even that the expression
got the test right in any or all cases.

OK, replace the 1 by (num!=atan(num))
For example, we commonly speak of "testing whether a number is prime"
and in a number of cases are satisfied if the test is looking for
"obvious" non-primes, something fast as a filter to keep us from
having to use slower but more complete algorithms.

Oh, you were thinking of the less accurate test being faster. <shrug>
Well, that was not in the original spec either.
 
F

Flash Gordon

Kenneth Brody wrote, On 10/07/07 18:52:
Walter 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] [...]
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.
But the OP did specify that the expression be one-line. Even if we
were to restrict our use of "number" to "the numbers representable
in C", since C does not have any run-time typing (RTT), I don't think
it would be possible to write a one-line expression that would take
an arbitrary number (of -any- of the valid C numeric types) as parameter
and return any meaningful information about it. If we could at least
receive the number as a void* and a type indicator, we could unionize
and ?: the problem to death.

Why not a macro?

#define POWEROF2(value) (1)

However, can zero be expressed as a power of 2? (I suppose raising 2
to the power of negative infinity may qualify, but I'm not certain.)

You could check for 0 then.
#define POWEROF2(value) ((value)!=0)

Hard to do something that will cope with inf and NaN for both complex
and doubles in C99, but we could lobby for adding type-generic versions
of isnan and isinf to C0x to solve this problem.
 
K

Keith Thompson

Flash Gordon said:
Kenneth Brody wrote, On 10/07/07 18:52: [...]
Why not a macro?
#define POWEROF2(value) (1)
However, can zero be expressed as a power of 2? (I suppose raising 2
to the power of negative infinity may qualify, but I'm not certain.)

You could check for 0 then.
#define POWEROF2(value) ((value)!=0)

Or, more simply:

#define POWEROF2(value) (value)

Both forms have the interesting property of treating non-null pointers
as powers of 2.
 
F

Flash Gordon

Keith Thompson wrote, On 10/07/07 21:49:
Flash Gordon said:
Kenneth Brody wrote, On 10/07/07 18:52: [...]
Why not a macro?
#define POWEROF2(value) (1)
However, can zero be expressed as a power of 2? (I suppose raising 2
to the power of negative infinity may qualify, but I'm not certain.)
You could check for 0 then.
#define POWEROF2(value) ((value)!=0)

Or, more simply:

#define POWEROF2(value) (value)

Both forms have the interesting property of treating non-null pointers
as powers of 2.

Perhaps that will help those people who want to do strange things
because the believe (incorrectly) that pointers are just integers then :)
 
P

pete

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

int n_is_Power_of_two(long unsigned n)
{
return (n & n - 1) == 0 && n != 0;
}

int n_is_Power_of_four(long unsigned n)
{
return (n & n - 1) == 0 && n % 3 == 1;
}

int n_is_Power_of_eight(long unsigned n)
{
return (n & n - 1) == 0 && n % 7 == 1;
}
 
D

Daniel Rudy

At about the time of 7/7/2007 11:35 PM, ravi stated the following:
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

But I'm not Luke... I still have my right hand.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fsck more fsck yes spray umount sleep
 
A

Army1987

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,
The same argument about infinities also applies to zero. The
difference is just the sign of the exponent.
 
A

Army1987

Harald van Dijk wrote, On 08/07/07 23:37:
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, [snip]
The argument about infinities also applies to zero. The difference
is just the sign of the exponent.
 
F

Flash Gordon

Army1987 wrote, On 13/07/07 13:31:
Harald van Dijk wrote, On 08/07/07 23:37:
Flash Gordon wrote:
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, [snip]
The argument about infinities also applies to zero. The difference
is just the sign of the exponent.

I didn't say it could not, I just said it could be dealt with easily.

I also did not think about it ;-)
 

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

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top