I have read through this SO question about 32-bits, but what about 64-bit numbers? Should I just mask the upper and lower 4 bytes, perform the count on the 32-bits and then add them together?
I have read through this SO question about 32-bits, but what about 64-bit numbers?
Share
You can find 64 bit version here http://en.wikipedia.org/wiki/Hamming_weight
It is something like this
This is a 64 bit version of the code form here How to count the number of set bits in a 32-bit integer?
Using Joshua’s suggestion I would transform it into this:
EDIT: I found a bug while testing 32 bit version. I added missing parentheses. The sum should be done before bitwise &, in the last line
EDIT2 Added safer version for ulong