Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Power of 2

Power of 2

Scheduled Pinned Locked Moved C / C++ / MFC
question
38 Posts 12 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Ryan Binns

    Crikey. I thought this was well known. Just do this:

    if (x & (x-1))
    {
      // x is <edit>not</edit>a power of two
    }
    else
    {
      // x is <edit>not</edit> a power of two
    }

    sorry, got the two cases round the wrong way... :-O

    Ryan

    "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

    -- modified at 6:04 Monday 6th March, 2006

    N Offline
    N Offline
    Nibu babu thomas
    wrote on last edited by
    #18

    Great work! You've implemented Russel's idea!


    Nibu thomas Software Developer

    R 1 Reply Last reply
    0
    • S Stephen Hewitt

      This is by far the best suggestion so far - I'd wager it's impossible to beat this technique. Steve

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #19

      And yet it got the lowest votes of all of them. Funny, isn't it :)

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      S 1 Reply Last reply
      0
      • N Nibu babu thomas

        Great work! You've implemented Russel's idea!


        Nibu thomas Software Developer

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #20

        Actually, I've been using this for years doing embedded programming. Every clock cycle is precious ;)

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        N R 2 Replies Last reply
        0
        • R Ryan Binns

          And yet it got the lowest votes of all of them. Funny, isn't it :)

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #21

          I gave you a 5 - I would have left it at that but then I saw the low score and felt compelled to reply. Steve

          R 1 Reply Last reply
          0
          • R Ryan Binns

            Actually, I've been using this for years doing embedded programming. Every clock cycle is precious ;)

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #22

            Yeah, well said.


            Nibu thomas Software Developer

            1 Reply Last reply
            0
            • S Stephen Hewitt

              I gave you a 5 - I would have left it at that but then I saw the low score and felt compelled to reply. Steve

              R Offline
              R Offline
              Ryan Binns
              wrote on last edited by
              #23

              Thanks :) I don't really care. I assume it was because of my first sentence. I was just surprised that five people answered, but not one of them knew about this. I genuinely believed it was well known!

              Ryan

              "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

              S 1 Reply Last reply
              0
              • R Ryan Binns

                Thanks :) I don't really care. I assume it was because of my first sentence. I was just surprised that five people answered, but not one of them knew about this. I genuinely believed it was well known!

                Ryan

                "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #24

                Surely using the word "crikey" demands higher rather than lower scores! Steve

                R 1 Reply Last reply
                0
                • S Stephen Hewitt

                  Surely using the word "crikey" demands higher rather than lower scores! Steve

                  R Offline
                  R Offline
                  Ryan Binns
                  wrote on last edited by
                  #25

                  :laugh:!

                  Ryan

                  "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                  1 Reply Last reply
                  0
                  • T toxcct

                    we are on a standard C++ forum, not managed/CLI...

                    D Offline
                    D Offline
                    Divyang Mithaiwala
                    wrote on last edited by
                    #26

                    Hello v2.0, We are on visual C++ & my suggesion is not wrong for it.


                    Divyang Mithaiwala System Engineer & Software Developer

                    T 1 Reply Last reply
                    0
                    • D Divyang Mithaiwala

                      Hello v2.0, We are on visual C++ & my suggesion is not wrong for it.


                      Divyang Mithaiwala System Engineer & Software Developer

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #27

                      Divyang Mithaiwala wrote:

                      my suggesion is not wrong for it.

                      yes it is, it will not work on a MFC/Win32 project... it is only for managed C++ (which is not for this forum, but for the C++/CLI forum instead)

                      1 Reply Last reply
                      0
                      • R Ryan Binns

                        Actually, I've been using this for years doing embedded programming. Every clock cycle is precious ;)

                        Ryan

                        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                        R Offline
                        R Offline
                        RichardS
                        wrote on last edited by
                        #28

                        Hi Ryan, Thanks for the help. The code is great and the application is for an embedded -> PC interface. cheers Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

                        1 Reply Last reply
                        0
                        • R Ryan Binns

                          Crikey. I thought this was well known. Just do this:

                          if (x & (x-1))
                          {
                            // x is <edit>not</edit>a power of two
                          }
                          else
                          {
                            // x is <edit>not</edit> a power of two
                          }

                          sorry, got the two cases round the wrong way... :-O

                          Ryan

                          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                          -- modified at 6:04 Monday 6th March, 2006

                          R Offline
                          R Offline
                          RichardS
                          wrote on last edited by
                          #29

                          Hi Ryan, Is there is quick way for now making x a power of 2 after you have decided that it is not? regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

                          R C 2 Replies Last reply
                          0
                          • T toxcct

                            :sigh:

                            S Offline
                            S Offline
                            Stephen Hewitt
                            wrote on last edited by
                            #30

                            Modified version is still wrong - It will treat any integer as a power of 2. Steve

                            T 1 Reply Last reply
                            0
                            • S Stephen Hewitt

                              Modified version is still wrong - It will treat any integer as a power of 2. Steve

                              T Offline
                              T Offline
                              toxcct
                              wrote on last edited by
                              #31

                              i did not pretend to modified my post for a much working stuff...

                              1 Reply Last reply
                              0
                              • R RichardS

                                Hi All, Is there a fast way of checking to see if an int is a power of 2 (i.e. 2, 4, 8, 16, 1024)? I know the long way using a loop, but I was hoping for a simple way of doing this. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

                                D Offline
                                D Offline
                                David Crow
                                wrote on last edited by
                                #32

                                #define ISPOWER2(x) (!((x)&((x)-1)))


                                "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                                "There is no death, only a change of worlds." - Native American Proverb

                                1 Reply Last reply
                                0
                                • R Russell

                                  Read this function, it could be another start point! This function that I wrote last year find the bigger long, greater than the input value, that is a power of 2. And it is quite fast!:) unsigned long Next2Power(unsigned long x){ unsigned long y=1, x1=x; if(x==0) return 0; while(x1!=0){ x1>>=1; y<<=1; } y>>=1; if(y!=x) y<<=1; return y; }

                                  Have a nice code day ;)

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #33

                                  _Russell_ wrote:

                                  This function that I wrote last year find the bigger long, greater than the input value, that is a power of 2.

                                  Why not just use log2()?


                                  "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                                  "There is no death, only a change of worlds." - Native American Proverb

                                  R 1 Reply Last reply
                                  0
                                  • R RichardS

                                    Hi Ryan, Is there is quick way for now making x a power of 2 after you have decided that it is not? regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

                                    R Offline
                                    R Offline
                                    Ryan Binns
                                    wrote on last edited by
                                    #34

                                    RichardS wrote:

                                    Is there is quick way for now making x a power of 2 after you have decided that it is not?

                                    Not that I know of. I've never had to do this. Would you round up or down?

                                    Ryan

                                    "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                                    R 1 Reply Last reply
                                    0
                                    • R Ryan Binns

                                      RichardS wrote:

                                      Is there is quick way for now making x a power of 2 after you have decided that it is not?

                                      Not that I know of. I've never had to do this. Would you round up or down?

                                      Ryan

                                      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                                      R Offline
                                      R Offline
                                      RichardS
                                      wrote on last edited by
                                      #35

                                      Round up. So far I have a loop (for 32-bits) shifting 0x80000000 down until the & with x is successful, then shifting it back up by one. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

                                      1 Reply Last reply
                                      0
                                      • D David Crow

                                        _Russell_ wrote:

                                        This function that I wrote last year find the bigger long, greater than the input value, that is a power of 2.

                                        Why not just use log2()?


                                        "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                                        "There is no death, only a change of worlds." - Native American Proverb

                                        R Offline
                                        R Offline
                                        Russell
                                        wrote on last edited by
                                        #36

                                        Because my function is faster (I think) than log2(). But if I'm wrong let me know how have a faster function. And why. thanks

                                        Have a nice code day ;)

                                        D 1 Reply Last reply
                                        0
                                        • R Russell

                                          Because my function is faster (I think) than log2(). But if I'm wrong let me know how have a faster function. And why. thanks

                                          Have a nice code day ;)

                                          D Offline
                                          D Offline
                                          David Crow
                                          wrote on last edited by
                                          #37

                                          _Russell_ wrote:

                                          ...let me know how have a faster function.

                                          This one benchmarks faster than the one you've shown:

                                          unsigned long Next2Power(unsigned long x)
                                          {
                                          x--;
                                          x |= x >> 1;
                                          x |= x >> 2;
                                          x |= x >> 4;
                                          x |= x >> 8;
                                          x |= x >> 16;
                                          x++;

                                          return x;
                                          

                                          }


                                          "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                                          "There is no death, only a change of worlds." - Native American Proverb

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups