Velocity Reviews > test whether a number is a power of 2

# test whether a number is a power of 2

Ed Morton
Guest
Posts: n/a

 09-29-2003
On 9/28/2003 1:21 PM, Matt wrote:
> Give a one-line C expression to test whether a number is a power of 2.
> No loops allowed.

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

Regards,

Ed.

Jirka Klaue
Guest
Posts: n/a

 09-29-2003
Ed Morton wrote:
> On 9/28/2003 1:21 PM, Matt wrote:
>
>>Give a one-line C expression to test whether a number is a power of 2.
>>No loops allowed.

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

Ed Morton
Guest
Posts: n/a

 09-29-2003

On 9/29/2003 10:44 AM, Jirka Klaue wrote:
> Ed Morton wrote:
>
>>On 9/28/2003 1:21 PM, Matt wrote:
>>
>>
>>>Give a one-line C expression to test whether a number is a power of 2.
>>>No loops allowed.

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

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.

lawrence.jones@eds.com
Guest
Posts: n/a

 09-29-2003
Joona I Palaste <(E-Mail Removed)> wrote:
>
> ((((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

Peter Nilsson
Guest
Posts: n/a

 09-29-2003
Eric Sosman <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Matt wrote:
> >
> > 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.)

Why stop there? C99 has complex types.

--
Peter

CBFalconer
Guest
Posts: n/a

 09-30-2003
Ed Morton wrote:
> On 9/28/2003 1:21 PM, Matt wrote:
>
> > Give a one-line C expression to test whether a number is a
> > power of 2. No loops allowed.

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

^^^^^^^^^^^^
even

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.

Aishwarya
Guest
Posts: n/a

 09-30-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Dan Pop) wrote in message news:<bl9hsv\$368\$(E-Mail Removed)>...
...

> 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. Rajesh Jeba Anbiah
Guest
Posts: n/a

 10-01-2003
"jacob navia" <(E-Mail Removed)> wrote in message news:<bl7kgo\$d5e\$(E-Mail Removed)>...
> > 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.

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

---
"Silence is the only right answer for many wrong questions" --
G.K.Moopanaar, Indian Politician
http://guideme.itgo.com/atozofc/ - "A to Z of C" Project
Email: rrjanbiah-at-Y!com