test whether a number is a power of 2

M

Matt

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

Joona I Palaste

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

((((x&&!(x&(x-!!x)))+x)-x)^x)^x

What do I win?

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
 
J

jacob navia

isPow2 = x && !( (x-1) & x );

Algorithm:
If x is a power of two, it doesn't have any bits in common with x-1, since it
consists of a single bit on. Any positive power of two is a single bit, using
binary integer representation.

This means that we test if x-1 and x doesn't share bits with the and operator.

Of course, if x is zero (not a power of two) this doesn't hold, so we add an
explicit test for zero with xx && expression.

Negative powers of two (0.5, 0.25, 0.125, etc) could share this same property
in a suitable fraction representation. 0.5 would be 0.1, 0.250 would be 0.01,
0.125 would be 0.001 etc.

jacob
 
A

Aishwarya

Joona I Palaste said:
((((x&&!(x&(x-!!x)))+x)-x)^x)^x

What do I win?


hi Joona,

i know this works because i just tested it .. but help me understand
the algoritm please ..

Thanks and Regards
A.
 
E

Eric Sosman

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

true_or_false = a_number > 0;

(For example, 3.14159 ~= 2 ** 1.65149.)
 
D

Dan Pop

In said:
We're not here to do your homework for you.

OTOH, the solution is far too tricky for a homework assignment. It
requires a bit of knowledge that has precious little to do with C
programming.

Dan
 
D

Dan Pop

In said:
i know this works because i just tested it .. but help me understand
the algoritm please ..

Had you engaged your brain, you'd have easily noticed that one ^x cancels
the other and that -x cancels the +x, so you're left with

x&&!(x&(x-!!x))

Now, !!x is an obfuscated way of writing 1, when you know that x cannot
be zero (it was already tested), so, we're left with:

x&&!(x&(x-1))

The tricky bit is x&(x-1) which actually clears the least significant
bit that happens to be set. If the original number was a power of two,
it had only one bit set, so the result of x&(x-1) must be zero in such
cases.

The rest should be obvious.

Dan
 
J

Jirka Klaue

Aishwarya said:
i know this works because i just tested it .. but help me understand
the algoritm please ..

+x -x does nothing, ^x ^x does nothing.

remains:
x && !(x & (x - !!x))

!!x ist alway 1, because x can't be 0 at this point.

remains:
x && !(x & (x - 1))

Jirka
 
J

Jirka Klaue

Ed said:
if (!(x%2)) {
puts("x is a power of 2");
}

Are you implying that 42 is a power of 2 and 43 is not?
BTW: It's a three-liner and no C expression at all. ;-)

Jirka
 
E

Ed Morton

Are you implying that 42 is a power of 2 and 43 is not?
BTW: It's a three-liner and no C expression at all. ;-)

Jirka

I had intended !(x%2) to be the expression, the rest was demonstrating using it.
I'll go back to sleep now.... wake me next time you want to know if a number is
a multiple of 2 rather than a power of 2. Sorry 'bout that.

Ed.
 
L

lawrence.jones

Joona I Palaste said:
((((x&&!(x&(x-!!x)))+x)-x)^x)^x

What do I win?

Nothing. What if x is negative? What if it's not an integer? What if
the original poster provided a complete specification of the problem? :)

-Larry Jones

I kind of resent the manufacturer's implicit assumption
that this would amuse me. -- Calvin
 
A

Aishwarya

(e-mail address removed) (Dan Pop) wrote in message ...
Now, !!x is an obfuscated way of writing 1, when you know that x cannot
be zero (it was already tested)

this is exactly what i got stuck in :) i should have guessed it ...
thanks for the explanation though :)
 
R

R. Rajesh Jeba Anbiah

jacob navia said:
Algorithm:
If x is a power of two, it doesn't have any bits in common with x-1, since it
consists of a single bit on. Any positive power of two is a single bit, using
binary integer representation.

This means that we test if x-1 and x doesn't share bits with the and operator.

Of course, if x is zero (not a power of two) this doesn't hold, so we add an
explicit test for zero with xx && expression.

Few Mathematicians say any positive integer raised to the power minus
infinity is 0. That is,
n (pow) (-infinity) = 0

IOW, 2 (pow) 3 = 8, 2 (pow) 2 = 4, 2 (pow) 1 = 2, 2 (pow) 0 = 1,
and, 2 (pow) (-infinity) = 0.

Because of this religious reason, I would just write my macro as
(without 0 check)
#define ISPOWOF2( n ) ( ! ( n & (n-1) ) )

But, someone here in CLC told me that the above macro won't work all
the time. I know, it won't work for float; I know the necessity of
additional paranthesis for n. But, I couldn't see any other reasons.
Anybody have any good comments? TIA
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top