to test whether a number is a power of 2

Discussion in 'C Programming' started by ravi, Jul 8, 2007.

  1. ravi

    ravi Guest

    Give a one-line C expression to test whether a number is a power of 2.
    [No loops allowed]
     
    ravi, Jul 8, 2007
    #1
    1. Advertising

  2. 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?
     
    Martin Ambuhl, Jul 8, 2007
    #2
    1. Advertising

  3. ravi

    Default User Guest

    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
     
    Default User, Jul 8, 2007
    #3
  4. ravi

    CBFalconer Guest

    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
     
    CBFalconer, Jul 8, 2007
    #4
  5. ravi

    ravi Guest

    On Jul 8, 10:22 am, Martin Ambuhl <> 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
     
    ravi, Jul 8, 2007
    #5
  6. 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
     
    Erik de Castro Lopo, Jul 8, 2007
    #6
  7. 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
     
    Richard Heathfield, Jul 8, 2007
    #7
  8. ravi

    santosh Guest

    ravi wrote:

    > On Jul 8, 10:22 am, Martin Ambuhl <> 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.
     
    santosh, Jul 8, 2007
    #8
  9. ravi

    Army1987 Guest

    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 (?)
     
    Army1987, Jul 8, 2007
    #9
  10. ravi

    pete Guest

    [ot] Re: to test whether a number is a power of 2

    ravi wrote:
    >
    > On Jul 8, 10:22 am, Martin Ambuhl <> 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/anakin/ep5/father.wav

    --
    pete
     
    pete, Jul 8, 2007
    #10
  11. ravi

    CBFalconer Guest

    Richard Heathfield wrote:
    > Erik de Castro Lopo said:
    >> ravi wrote:
    >>
    >>> Give a one-line C expression to test whether a number is a power
    >>> of 2.

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


    I like my version better :) In all seriousness, this is not such
    a simple thing, so I doubt it was homework. If he hadn't used such
    rude terminology I would be virtually sure it is not.

    --
    <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
     
    CBFalconer, Jul 8, 2007
    #11
  12. ravi

    Eric Sosman Guest

    ravi wrote:
    > On Jul 8, 10:22 am, Martin Ambuhl <> 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


    That explains my congenital idiocy.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jul 8, 2007
    #12
  13. ravi

    pete Guest

    pete, Jul 8, 2007
    #13
  14. ravi

    Flash Gordon Guest

    Eric Sosman wrote, On 08/07/07 13:27:
    > ravi wrote:
    >> On Jul 8, 10:22 am, Martin Ambuhl <> 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

    >
    > That explains my congenital idiocy.


    Probably. Had you enough sense you would have instantly known the answer
    was:

    int powerof2( double num ) { return 1; }

    After all, the OP did not specify integer power of 2, or even real power
    of 2, and I think if you allow complex powers, all numbers will qualify.
    --
    Flash Gordon
     
    Flash Gordon, Jul 8, 2007
    #14
  15. Flash Gordon wrote:
    > Eric Sosman wrote, On 08/07/07 13:27:
    >> ravi wrote:
    >>> On Jul 8, 10:22 am, Martin Ambuhl <> 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

    >>
    >> That explains my congenital idiocy.

    >
    > Probably. Had you enough sense you would have instantly known the answer
    > was:
    >
    > int powerof2( double num ) { return 1; }
    >
    > After all, the OP did not specify integer power of 2, or even real power
    > of 2, and I think if you allow complex powers, all numbers will qualify.


    What about zeroes, infinities, and NaNs? I would think it should return 0
    for those.
     
    Harald van =?UTF-8?B?RMSzaw==?=, Jul 8, 2007
    #15
  16. ravi

    Flash Gordon Guest

    Harald van Dijk wrote, On 08/07/07 23:37:
    > Flash Gordon wrote:
    >> Eric Sosman wrote, On 08/07/07 13:27:
    >>> ravi wrote:
    >>>> On Jul 8, 10:22 am, Martin Ambuhl <> 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
    >>> That explains my congenital idiocy.

    >> Probably. Had you enough sense you would have instantly known the answer
    >> was:
    >>
    >> int powerof2( double num ) { return 1; }
    >>
    >> After all, the OP did not specify integer power of 2, or even real power
    >> of 2, and I think if you allow complex powers, all numbers will qualify.

    >
    > What about zeroes, infinities, and NaNs? I would think it should return 0
    > for those.


    Zero is easy to deal with, infinities could be argued as being powers of
    2, and change the input parameter to an integer type to get rid of the
    problem of NaNs :) Alternatively, in C99 you can use isnan and isinf to
    solve the problem.

    int powerof2(double num) {return !(isnan(num) || isinf(num) || num==0)}

    There is, of course, the question of whether you want to count numbers
    that are almost zero as zero because of rounding, but I'll leave dealing
    with that as an exercise to anyone wanting that functionality.
    --
    Flash Gordon
     
    Flash Gordon, Jul 9, 2007
    #16
  17. CBFalconer <> wrote:

    [OP wrote:]

    > >>> Give a one-line C expression to test whether a number is a power
    > >>> of 2.


    > I like my version better :) In all seriousness, this is not such
    > a simple thing, so I doubt it was homework. If he hadn't used such
    > rude terminology I would be virtually sure it is not.


    Given the number of ridiculous questions that are routinely asked here
    that clearly *are* homework, I'm surprised you find it unlikely that
    the above question is not homework. Why else would there be
    constraints such as "one line" or "no loops allowed" (snipped from
    above context)? I personally find it highly unlikely that anyone
    asking the question seriously would phrase it so tersely.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jul 9, 2007
    #17
  18. ravi

    CBFalconer Guest

    Christopher Benson-Manica wrote:
    > CBFalconer <> wrote:
    > [OP wrote:]
    >
    >>> Give a one-line C expression to test whether a number is a power
    >>> of 2.

    >
    >> I like my version better :) In all seriousness, this is not such
    >> a simple thing, so I doubt it was homework. If he hadn't used such
    >> rude terminology I would be virtually sure it is not.

    >
    > Given the number of ridiculous questions that are routinely asked here
    > that clearly *are* homework, I'm surprised you find it unlikely that
    > the above question is not homework. Why else would there be
    > constraints such as "one line" or "no loops allowed" (snipped from
    > above context)? I personally find it highly unlikely that anyone
    > asking the question seriously would phrase it so tersely.


    You have a definite point, and are probably right.

    --
    <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
     
    CBFalconer, Jul 9, 2007
    #18
  19. ravi

    Guest

    On Jul 8, 5:54 am, Army1987 <> wrote:
    > 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)?


    Oh for crying out loud! The other answers had the OP perfectly set up
    with the wrong answer (brilliant coordination, BTW, guys -- all of you
    getting it simultaneously wrong in exactly the same way). Now the OP
    can figure it out completely correctly without any research himself.
    Good job.

    --
    Paul Hsieh
    http://www.pobox.com/~qed/
    http://bstring.sf.net/
     
    , Jul 9, 2007
    #19
  20. On 8 Jul, 23:37, Harald van D k <> wrote:
    > Flash Gordon wrote:
    > > Eric Sosman wrote, On 08/07/07 13:27:
    > >> ravi wrote:
    > >>> On Jul 8, 10:22 am, Martin Ambuhl <> wrote:
    > >>>> ravi wrote:


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


    <snip>

    > > [...] Had you enough sense you would have instantly known the answer
    > > was:

    >
    > > int powerof2( double num ) { return 1; }

    >
    > > After all, the OP did not specify integer power of 2, or even real power
    > > of 2, and I think if you allow complex powers, all numbers will qualify.

    >
    > What about zeroes, infinities, and NaNs? I would think it should return 0
    > for those


    ah, but the spec said test a *number* and I don't think infinity is a
    number and as for NaN...

    So it should indicate some sort of domain error or assert on a debug
    build.


    --
    Nick Keighley
     
    Nick Keighley, Jul 10, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt

    test whether a number is a power of 2

    Matt, Sep 28, 2003, in forum: C Programming
    Replies:
    17
    Views:
    12,609
    R. Rajesh Jeba Anbiah
    Oct 1, 2003
  2. MJ
    Replies:
    11
    Views:
    1,106
  3. David T. Ashley
    Replies:
    10
    Views:
    654
    Old Wolf
    Jan 5, 2007
  4. Replies:
    8
    Views:
    395
    Mark Dickinson
    Apr 17, 2008
  5. Erik the Red
    Replies:
    4
    Views:
    193
    Chris Pine
    Jul 29, 2005
Loading...

Share This Page