CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2007
    Posts
    1

    Will you solve this tricky question?

    how would you decide a number is some power of 2 in c? no loops are allowed and this should be done in one statement.
    Last edited by RAJJUMOTE; May 26th, 2007 at 01:34 PM.

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Will you solve this tricky question?

    Code:
    bool IsPowerOfTwo (int number)
    {
    return number ? ((number & -number)==number):false;
    }
    Laitinen

  3. #3
    Join Date
    Oct 1999
    Location
    ks
    Posts
    524

    Re: Will you solve this tricky question?

    double SomeNumber, SomeExponent;

    SomeNumber = 2**SomeExponent;
    log(SomeNumber) = SomeExponent * log(2);
    SomeExponent = log(SomeNumber) / log(2);

    is SomeExponent an integer?

    in theory:

    if SomeExponent - floor(SomeExponent) = 0;

    in practice:

    double epsilon;
    epsilon = .00001;
    if abs( SomeExponent - floor(SomeExponent) ) < epsilon;

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Will you solve this tricky question?

    Pretty cool solution Latinen.

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Will you solve this tricky question?

    Folks, we should be determining that this is not a homework question, or that the OP has at least attempted it, before answering. The form of the OP certainly suggests that it is.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Will you solve this tricky question?

    Quote Originally Posted by S_M_A
    Pretty cool solution Latinen.
    For integers, there's an even simpler solution.

    (ETA: I suppose I should say conceptually simpler...)
    Last edited by Graham; May 27th, 2007 at 10:52 AM.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Jun 2002
    Posts
    137

    Re: Will you solve this tricky question?

    let me remove my codes.
    Last edited by sandodo; May 28th, 2007 at 12:23 AM.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Will you solve this tricky question?

    Quote Originally Posted by RAJJUMOTE
    how would you decide a number is some power of 2 in c? no loops are allowed and this should be done in one statement.
    Note that it's always the person with only 1 or 2 posts that asks these kind of questions.

    Definitely a homework question, since the OP has not appeared since he now has something to take back to the teacher. We should be much better sniffing out "do my homework" posts, and not provide answers.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Will you solve this tricky question?

    Quote Originally Posted by Paul McKenzie
    Note that it's always the person with only 1 or 2 posts that asks these kind of questions.

    Definitely a homework question, since the OP has not appeared since he now has something to take back to the teacher. We should be much better sniffing out "do my homework" posts, and not provide answers.

    Regards,

    Paul McKenzie
    See signature below...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured