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

Can anyone point me to it please or provide an algorithm. Thanks

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

Thanks. That is the very thing.

Many more thanks... These links are great too.