**Content originally posted in LPCWare by dhenry on Tue Oct 04 15:28:07 MST 2011**

I have been trying to do this for the last week or so.

How do you quickly and efficiently count the number of 1s in a 32-bit data type (signed long for example)?

For example, 0xf2 would have 4 + 1 = 5 1s. 0x23f4 has 1 + 2 + 4 + 1 = 8 1s. And so on and so forth.

How would you do it?

Thank you very much.

Content originally posted in LPCWare by cmcquaid on Tue Oct 04 17:39:44 MST 2011http://en.wikipedia.org/wiki/Hamming_weight

or you could use CLZ (count leading zeroes) and subtract powers of two, if you want to do it in asm.