Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > test whether a number is a power of 2

Reply
Thread Tools

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.

 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
 
 
 
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.

 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Continuous beeps on my computer.. whether power on or off alkon Computer Information 2 07-21-2007 04:16 AM
to test whether a number is a power of 2 ravi C Programming 31 07-13-2007 04:27 PM
The while loop for calculating a power of a number less than another number? Erik the Red Ruby 4 07-29-2005 08:28 PM
how to find a number whether its a power of 2 or not MJ C Programming 11 04-29-2005 10:44 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments