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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Finding number of bits set in a given number

Finding number of bits set in a given number

Scheduled Pinned Locked Moved C / C++ / MFC
question
23 Posts 8 Posters 1 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.
  • G Gary R Wheeler

    unsigned int sample; // value to count bits in
    unsigned int count = ((sample >> 31) & 0x00000001) +
    ((sample >> 30) & 0x00000001) +
    ((sample >> 29) & 0x00000001) +
    ((sample >> 28) & 0x00000001) +
    ((sample >> 27) & 0x00000001) +
    ((sample >> 26) & 0x00000001) +
    ((sample >> 25) & 0x00000001) +
    ((sample >> 24) & 0x00000001) +
    ((sample >> 23) & 0x00000001) +
    ((sample >> 22) & 0x00000001) +
    ((sample >> 21) & 0x00000001) +
    ((sample >> 20) & 0x00000001) +
    ((sample >> 19) & 0x00000001) +
    ((sample >> 18) & 0x00000001) +
    ((sample >> 17) & 0x00000001) +
    ((sample >> 16) & 0x00000001) +
    ((sample >> 15) & 0x00000001) +
    ((sample >> 14) & 0x00000001) +
    ((sample >> 13) & 0x00000001) +
    ((sample >> 12) & 0x00000001) +
    ((sample >> 11) & 0x00000001) +
    ((sample >> 10) & 0x00000001) +
    ((sample >> 9) & 0x00000001) +
    ((sample >> 8) & 0x00000001) +
    ((sample >> 7) & 0x00000001) +
    ((sample >> 6) & 0x00000001) +
    ((sample >> 5) & 0x00000001) +
    ((sample >> 4) & 0x00000001) +
    ((sample >> 3) & 0x00000001) +
    ((sample >> 2) & 0x00000001) +
    ((sample >> 1) & 0x00000001) +
    (sample & 0x00000001);

    Of course, this assumes that you know that an unsigned int occupies 32 bits.


    Software Zen: delete this;

    Fold With Us![^]

    G Offline
    G Offline
    Gary R Wheeler
    wrote on last edited by
    #14

    Actually, this is probably a reasonably fast solution, at least on Intel hardware. Bit shifts are done with a barrel shifter, which means all of the >> operations are done only in a 4-10 clock cycles IIRC. The & operations are similar. This means the entire expression could be evaluated in a few hundred clocks. Yes, it's verbose. It has the advantage of simplicity, doesn't require a global table, and could be inlined if necessary.


    Software Zen: delete this;

    Fold With Us![^]

    1 Reply Last reply
    0
    • T toxcct

      because it expands the bits to the array, and as the minimum unit that can be addressed is the byte (not the bit), then it might slower the treatment... i'd recommend using bitarray when it's really needed, for huge bits operations for instance (refering to bjarne stroustrup reflection on the subject)...

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

      v2.0 wrote:

      because it expands the bits to the array, and as the minimum unit that can be addressed is the byte

      That is not how bitsets work. They store the data as bits and use bit operations ("and" & "or", etc) to address them. It has an embedded class called reference that makes this seamless but it does happen. Here's some code from MSVC6's implementation (I altered the formatting):

      bitset<_N>& set(size_t _P, bool _X = true)
      {
           if (_N <= _P)
               _Xran();
           if (_X)
               A[_P / _Nb] |= (_Ty)1 << _P % _Nb;
          else
              _A[_P / _Nb] &= ~((_Ty)1 << _P % _Nb);
          return (*this);
      }
      

      This is the whole idea behind bitsets - To have the notational convince without space overheads you alluded to above. Steve

      T 1 Reply Last reply
      0
      • S Stephen Hewitt

        v2.0 wrote:

        because it expands the bits to the array, and as the minimum unit that can be addressed is the byte

        That is not how bitsets work. They store the data as bits and use bit operations ("and" & "or", etc) to address them. It has an embedded class called reference that makes this seamless but it does happen. Here's some code from MSVC6's implementation (I altered the formatting):

        bitset<_N>& set(size_t _P, bool _X = true)
        {
             if (_N <= _P)
                 _Xran();
             if (_X)
                 A[_P / _Nb] |= (_Ty)1 << _P % _Nb;
            else
                _A[_P / _Nb] &= ~((_Ty)1 << _P % _Nb);
            return (*this);
        }
        

        This is the whole idea behind bitsets - To have the notational convince without space overheads you alluded to above. Steve

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

        and don't you think this is comsuming more cpu ? firstly, you call a function, so unless it is inlined, there's all the call stack stuff that comes to work. then each operation is actually a call to an operator function, and in a line like _A[_P / _Nb] &= ~((_Ty)1 << _P % _Nb), you have about 7 operators called !!! do you see what i want to show off ?

        S 1 Reply Last reply
        0
        • T toxcct

          :wtf: that's what we call hack ?! :-D [OT] seems that you didn't find back the mail i sent you last week... so here is a summary : it is about your article on SADirRead. i wanted to have your autorization to make some few code refactor, and mostly including some documentation (like Doxygen) on the methods/members, and send you back the zips to update the article... there also was a little bug in the code, but i don't remember which (if you can find back the mail). i'm waiting for your answer... you can email me, i don't have any that aggressive spam filter :-D [/OT]

          B Offline
          B Offline
          Blake Miller
          wrote on last edited by
          #17

          Compared to your original answer, this is a BETTER solution to the problem, because here are the original requirements: How do I find the number of bits set in a given integer number without using any while/for loop I added the emphasis ;) So, if you were a civil engineer and I wanted a brigde, would you construct me a dam or a building instead? The author wanted an answer that did not use a for or while loop. IF that was not the constraint, I would have come up with something like you suggested ... :doh: People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks

          T 1 Reply Last reply
          0
          • B Blake Miller

            Compared to your original answer, this is a BETTER solution to the problem, because here are the original requirements: How do I find the number of bits set in a given integer number without using any while/for loop I added the emphasis ;) So, if you were a civil engineer and I wanted a brigde, would you construct me a dam or a building instead? The author wanted an answer that did not use a for or while loop. IF that was not the constraint, I would have come up with something like you suggested ... :doh: People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks

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

            oops, effectively, i missed that point... thanks :)

            1 Reply Last reply
            0
            • T toxcct

              myInt mi = /*...*/; //The integer to scan

              int iCpt = 0; //The counter of set bits

              //For each bits in the integer
              for (int i = 0; i < sizeof(mi) * 8; i++) {

              //If the most right bit is set
              if ((mi & 0x1) == 0x1) {
              
                  //Increasing the counter
                  iCpt++;
              
              }
              
              //shifting all bits of 1 position to the right
              mi >> 1;
              

              }

              printf("mi contains %d '1'\n", iCpt);

              ps: don't cross posts the forums. delete your question on the C++/CLI forum, as it is not a managed C++ question -- modified at 6:51 Tuesday 7th March, 2006 (thanks stephen)

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

              v2.0 wrote:

              for (int i = 0; i < sizeof(mi) * 8; i++) {

              I believe that amar specifically indicated no while or for loops.


              "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

              T 1 Reply Last reply
              0
              • D David Crow

                v2.0 wrote:

                for (int i = 0; i < sizeof(mi) * 8; i++) {

                I believe that amar specifically indicated no while or for loops.


                "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

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

                yes, badKarma already notified me about that point that i did not see firstly

                1 Reply Last reply
                0
                • A Amar Sutar

                  How do I find the number of bits set in a given integer number without using any while/for loop? :confused:

                  B Offline
                  B Offline
                  BadKarma
                  wrote on last edited by
                  #21

                  This works also, and its pretty fast ;P

                    int A = 0xAA000000;
                    int B;
                    __asm
                    {
                      mov eax, dword ptr [A] ;
                      mov ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      MOV dword ptr [B],ebx ;
                    }
                  

                  I know the following is better but he said no loops

                    int A = 0xAA000000;
                    int B;
                  
                    __asm
                    {
                      mov eax, dword ptr [A] ;
                      mov ebx,0 ;
                      here:
                      SHL eax,1 ;
                      ADC ebx,0 ;
                      CMP eax, 0
                      JNE here
                      MOV dword ptr [B],ebx ;
                    }
                  

                  codito ergo sum

                  1 Reply Last reply
                  0
                  • T toxcct

                    and don't you think this is comsuming more cpu ? firstly, you call a function, so unless it is inlined, there's all the call stack stuff that comes to work. then each operation is actually a call to an operator function, and in a line like _A[_P / _Nb] &= ~((_Ty)1 << _P % _Nb), you have about 7 operators called !!! do you see what i want to show off ?

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

                    The functions are inline - functions which are defined and not just declared in the class definition are implicitly inline. The operators involved compile down to very simple machine code instructions, for example:   /32 : sar eax,5   %32 : and eax,0000001Fh For a function which sets or clears a numbered bit there is no avoiding the &=, |= and << operators. As I said the MSVC6 implementation could be better:  - The constructors are not efficient.  - There is no specialization for the cases when the number of bits can fit into a machine word. If there was we could avoid the [], / and % operations in these cases in the set method. My main point however was to point out that the bitset doesn’t expand the data out to one byte per bit as you suggested. Also with a good implementation of STL and a decent compiler (I haven’t look under the hood of MCVC7’s STL) using a bitset is free (or close to). Steve

                    B 1 Reply Last reply
                    0
                    • S Stephen Hewitt

                      The functions are inline - functions which are defined and not just declared in the class definition are implicitly inline. The operators involved compile down to very simple machine code instructions, for example:   /32 : sar eax,5   %32 : and eax,0000001Fh For a function which sets or clears a numbered bit there is no avoiding the &=, |= and << operators. As I said the MSVC6 implementation could be better:  - The constructors are not efficient.  - There is no specialization for the cases when the number of bits can fit into a machine word. If there was we could avoid the [], / and % operations in these cases in the set method. My main point however was to point out that the bitset doesn’t expand the data out to one byte per bit as you suggested. Also with a good implementation of STL and a decent compiler (I haven’t look under the hood of MCVC7’s STL) using a bitset is free (or close to). Steve

                      B Offline
                      B Offline
                      BadKarma
                      wrote on last edited by
                      #23

                      If you realy worry about cpu cycles you should write in asm or at least in inline assembly See my post here[^] I agree that in most situation the bitset will prefrom more then adequate. codito ergo sum -- modified at 18:28 Tuesday 7th March, 2006

                      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