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.