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