Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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]

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


Do your own homework.



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

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


Beacuse I am your father

 
Reply With Quote
 
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
 
Reply With Quote
 
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@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
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
trying to remain civil), to get the best response and help. At least
that been my experience in my participation in this group.

 
Reply With Quote
 
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 (?)

 
Reply With Quote
 
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
 
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
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 whether a number is a power of 2 Matt C Programming 17 10-01-2003 05:00 AM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments