Home |  About Us |  Help |  Site Map |  Contact
Assembly Snippet bitCount
Design Criteria : speed
; *** Count the number of bits in DWORD
bitCount proc STDCALL num:DWORD

  sub eax,eax  ; clear a bit counter
  mov ecx,eax  ; zero
  add ecx,num  ; add the number to be bit counted
  je  noBits   ; jump and exit if no bits
bitLp:
  inc eax      ; count one bit
  mov ebx,ecx  ; get number to bit count
  dec ebx      ; invert all LSB's
  and ecx,ebx  ; clear all the inverted bits
  jne bitLp    ; loop if more bits
noBits:
  ret          ; return with bit count in eax

bitCount endp
pseudocode
while number > 0
    count = count + 1
    number = ( number -1 ) and number
end while
More

Related




Home |  About Us |  Help |  Site Map |  Contact