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

# to test whether a number is a power of 2

ravi
Guest
Posts: n/a

 07-08-2007
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]

Martin Ambuhl
Guest
Posts: n/a

 07-08-2007
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?

Default User
Guest
Posts: n/a

 07-08-2007
ravi wrote:

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

Brian

CBFalconer
Guest
Posts: n/a

 07-08-2007
ravi wrote:
>
> Give a one-line C expression to test whether a number is a power
> of 2. [No loops allowed]

For unsigned integers:

if (!((n - 1) & n)) puts("n is power of 2");

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

ravi
Guest
Posts: n/a

 07-08-2007
On Jul 8, 10:22 am, Martin Ambuhl <(E-Mail Removed)> 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?

Erik de Castro Lopo
Guest
Posts: n/a

 07-08-2007
ravi wrote:

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

static inline int
is_power_of_2 (int x)
{
return ((x & (x - 1)) == 0) ;
}

--
-----------------------------------------------------------------
Erik de Castro Lopo
-----------------------------------------------------------------
"Hamas: Islam will conquer US and Britain."
-- http://www.pmw.org.il/LatestBulletins.htm#b220606

Richard Heathfield
Guest
Posts: n/a

 07-08-2007
Erik de Castro Lopo said:

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

>
> static inline int
> is_power_of_2 (int x)
> {
> return ((x & (x - 1)) == 0) ;
> }

Having proved willing to do homework for free, you and Chuck might want
to brace yourselves for the rush.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

santosh
Guest
Posts: n/a

 07-08-2007
ravi wrote:

> On Jul 8, 10:22 am, Martin Ambuhl <(E-Mail Removed)> 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

Most participants of this group an, (not unreasonable), expectation of
people who post homework questions to have at least made an honest
attempt at the problem. This group hosts, perhaps, the best minds in C
you're likely to meet, but you, (as a new participant and a learner),
need to show willingness to put in some effort on your part, (like
that been my experience in my participation in this group.

Army1987
Guest
Posts: n/a

 07-08-2007
On Sun, 08 Jul 2007 17:23:03 +1000, Erik de Castro Lopo wrote:

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

>
> static inline int
> is_power_of_2 (int x)
> {
> return ((x & (x - 1)) == 0) ;

What if x <= 0 (in which case x cannot be a power of two)?
> }

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

pete
Guest
Posts: n/a

 07-08-2007
ravi wrote:
>
> On Jul 8, 10:22 am, Martin Ambuhl <(E-Mail Removed)> 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

http://www.dockingbay101.com/sounds/...ep5/father.wav

--
pete