I'm sure there was a page showing how to count the number of binary 1s in a 32bit number.

Can anyone point me to it please or provide an algorithm. Thanks
Posted on 2004-08-01 12:52:07 by V Coder
This macro counts the no of 1's in esi I believe, someone posted it here once.
POPCOUNT1 MACRO

mov edx,esi
shr edx,1
and edx,055555555h ; (n >> 1) & 0x55555555
sub esi,edx ; n - ((n >> 1) & 0x55555555)
mov edx,esi
shr edx,2 ; n >> 2
and esi,033333333h ; n & 0x33333333
and edx,033333333h ; (n >> 2) & 0x33333333
add esi,edx ; n = (n & 0x33333333) + ((n >> 2) & 0x33333333)
mov edx,esi
shr edx,4 ; n >> 4
add edx,esi ; n + (n >> 4)
and edx,00F0F0F0Fh ; n = (n + (n >> 4) & 0x0F0F0F0F)

imul edx,001010101h ; add by multiplying with a "magic number"
shr edx,24 ; shift result into place
ENDM
Posted on 2004-08-01 13:19:22 by Eóin
Thanks. That is the very thing.
Posted on 2004-08-01 13:59:48 by V Coder
Posted on 2004-08-01 14:01:40 by f0dder
Many more thanks... These links are great too.
Posted on 2004-08-05 00:10:39 by V Coder