A C Question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack Young

    #1

    A C Question.

    Hi:
    There is question about C programming:
    Use just one C statement to check whether a variable is the power of 2. No
    loops allowed.
    Waiting for your answer.I have not slept well a few days for this.


  • Eric Sosman

    #2
    Re: A C Question.

    Jack Young wrote:[color=blue]
    > Hi:
    > There is question about C programming:
    > Use just one C statement to check whether a variable is the power of 2. No
    > loops allowed.
    > Waiting for your answer.I have not slept well a few days for this.[/color]

    int is_power_of_two (double x) {
    return x > 0.0; /* one statement, no loops */
    }

    Sweet dreams!

    --
    Eric Sosman
    esosman@acm-dot-org.invalid

    Comment

    • jmazzi

      #3
      Re: A C Question.

      if ( var % 2 == 0 ) {
      printf("yep\n") ;
      }

      Comment

      • Robert Gamble

        #4
        Re: A C Question.

        Jack Young wrote:[color=blue]
        > Hi:
        > There is question about C programming:
        > Use just one C statement to check whether a variable is the power of 2. No
        > loops allowed.
        > Waiting for your answer.I have not slept well a few days for this.[/color]

        puts(x & (x-1) || x==0 ? "not a power of two" : "power of two");

        Robert Gamble

        Comment

        • Robert Gamble

          #5
          Re: A C Question.

          PLEASE read how to properly post via Google Groups at
          http://cfaj.freeshell.org/google/ before attempting to do so again.

          jmazzi wrote:[color=blue]
          > if ( var % 2 == 0 ) {
          > printf("yep\n") ;
          > }[/color]

          The OP asked how to determine if a variable was a *power* of two, not a
          *multiple* of two.

          Robert Gamble

          Comment

          • jmazzi

            #6
            Re: A C Question.

            my bad ;)

            Comment

            • Keith Thompson

              #7
              Re: A C Question.

              "jmazzi" <jmazzi@gmail.c om> writes:[color=blue]
              > my bad ;)[/color]

              Please read <http://cfaj.freeshell. org/google/>. Thanks.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Logan Shaw

                #8
                Re: A C Question.

                Eric Sosman wrote:[color=blue]
                > Jack Young wrote:[color=green]
                >> There is question about C programming:
                >> Use just one C statement to check whether a variable is the power of
                >> 2. No loops allowed.
                >> Waiting for your answer.I have not slept well a few days for this.[/color][/color]
                [color=blue]
                > int is_power_of_two (double x) {
                > return x > 0.0; /* one statement, no loops */
                > }[/color]

                Floating point numbers (at least IEEE and similar ones) can be expressed
                as a fraction whose denominator is a power of two. But that is not the
                same thing as the number itself being a power of two. For instance,
                3.0 is not a power of two.

                - Logan

                Comment

                • Keith Thompson

                  #9
                  Re: A C Question.

                  Logan Shaw <lshaw-usenet@austin.r r.com> writes:[color=blue]
                  > Eric Sosman wrote:[color=green]
                  >> Jack Young wrote:[color=darkred]
                  >>> There is question about C programming:
                  >>> Use just one C statement to check whether a variable is the power
                  >>> of 2. No loops allowed.
                  >>> Waiting for your answer.I have not slept well a few days for
                  >>> this.[/color][/color]
                  >[color=green]
                  >> int is_power_of_two (double x) {
                  >> return x > 0.0; /* one statement, no loops */
                  >> }[/color]
                  >
                  > Floating point numbers (at least IEEE and similar ones) can be expressed
                  > as a fraction whose denominator is a power of two. But that is not the
                  > same thing as the number itself being a power of two. For instance,
                  > 3.0 is not a power of two.[/color]

                  Sure it is; the exponent is approximately 1.58496.

                  The OP is expecting us to do his homework for him without even
                  providing a precise statement of the problem.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • pete

                    #10
                    Re: A C Question.

                    Jack Young wrote:[color=blue]
                    >
                    > Hi:
                    > There is question about C programming:
                    > Use just one C statement to check whether
                    > a variable is the power of 2.
                    > No loops allowed.
                    > Waiting for your answer.I have not slept well a few days for this.[/color]

                    int n_is_Power_of_t wo(long unsigned n)
                    {
                    return (n & n - 1) == 0 && n != 0;
                    }

                    int n_is_Power_of_f our(long unsigned n)
                    {
                    return (n & n - 1) == 0 && n % 3 == 1;
                    }

                    int n_is_Power_of_e ight(long unsigned n)
                    {
                    return (n & n - 1) == 0 && n % 7 == 1;
                    }

                    --
                    pete

                    Comment

                    • jat

                      #11
                      Re: A C Question.

                      modf(log(a)/log(2),&b)?0:1;

                      i have tried this on C++. I m not sure whether the functions are
                      available in C.
                      but the logic is that if we get a integer after taking the log of given
                      number with respect to 2(or any number), it is divisible.

                      n power x = y
                      x log(n) = log(y)
                      x = log(y)/log(n)

                      it may seem abstruse , but it's just another solution.

                      Comment

                      • Ian Collins

                        #12
                        Re: A C Question.

                        jat wrote:[color=blue]
                        > modf(log(a)/log(2),&b)?0:1;
                        >
                        > i have tried this on C++. I m not sure whether the functions are
                        > available in C.
                        > but the logic is that if we get a integer after taking the log of given
                        > number with respect to 2(or any number), it is divisible.
                        >
                        > n power x = y
                        > x log(n) = log(y)
                        > x = log(y)/log(n)
                        >
                        > it may seem abstruse , but it's just another solution.
                        >[/color]
                        Imagine waiting for this to run on an 8 bit microcontroller !

                        --
                        Ian Collins.

                        Comment

                        • Keith Thompson

                          #13
                          Re: A C Question.

                          "jat" <contactravidal al@gmail.com> writes:[color=blue]
                          > modf(log(a)/log(2),&b)?0:1;
                          >
                          > i have tried this on C++. I m not sure whether the functions are
                          > available in C.
                          > but the logic is that if we get a integer after taking the log of given
                          > number with respect to 2(or any number), it is divisible.
                          >
                          > n power x = y
                          > x log(n) = log(y)
                          > x = log(y)/log(n)
                          >
                          > it may seem abstruse , but it's just another solution.[/color]

                          Another solution to what? The question was about determing whether a
                          number is a power of two, but we can't necessarily see the article to
                          which you're replying. Please read <http://cfaj.freeshell. org/google/>.

                          The method may be mathematically valid, but since floating-point
                          numbers are inexact, it's likely to fail in many cases.

                          But since the original poster was probably asking us to do his
                          homework for him, giving him *correct* answers is a bad idea.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                          We must do something. This is something. Therefore, we must do this.

                          Comment

                          • Jordan Abel

                            #14
                            Re: A C Question.

                            On 2006-01-18, Logan Shaw <lshaw-usenet@austin.r r.com> wrote:[color=blue]
                            > Eric Sosman wrote:[color=green]
                            >> Jack Young wrote:[color=darkred]
                            >>> There is question about C programming:
                            >>> Use just one C statement to check whether a variable is the power of
                            >>> 2. No loops allowed.
                            >>> Waiting for your answer.I have not slept well a few days for this.[/color][/color]
                            >[color=green]
                            >> int is_power_of_two (double x) {
                            >> return x > 0.0; /* one statement, no loops */
                            >> }[/color]
                            >
                            > Floating point numbers (at least IEEE and similar ones) can be expressed
                            > as a fraction whose denominator is a power of two. But that is not the
                            > same thing as the number itself being a power of two. For instance,
                            > 3.0 is not a power of two.[/color]

                            Sure it is.

                            2^1.58496250072 115618146

                            oh, you meant an integer power of two?

                            Comment

                            • Nelu

                              #15
                              Re: A C Question.

                              jmazzi wrote:[color=blue]
                              > if ( var % 2 == 0 ) {
                              > printf("yep\n") ;
                              > }
                              >[/color]
                              Since when is 6 a power of 2?
                              If you're allowed to have real powers than 7 is a power of 2 and doesn't
                              satisfy your condition. Either way, you're either catching too many numbers
                              or not enough :-).
                              Shorter: an even number is not necessarily a power of 2.

                              --
                              Ioan - Ciprian Tandau
                              tandau _at_ freeshell _dot_ org (hope it's not too late)
                              (... and that it still works...)

                              Comment

                              Working...