counting 1s in a 32-bit type

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
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.