Quote:Original post by Anonymous PosterThe fastest method is that assembly instruction that counts the number of set bits in a 32-bit value. No idea what it's called though...
Indeed, the fastest method is via a look-up table.
finding the number of SET bits in one line of code
Quote:Original post by Evil Steve
The fastest method is that assembly instruction that counts the number of set bits in a 32-bit value. No idea what it's called though...
You are thinking of BSR which computes the integer log base 2. I don't know of an instruction which counts the number of set bits.
Based on the 64-bit versions on the page linked to by jpetrie and assuming an 8-bit byte and 32-bit unsigned int:
Σnigma
int countbits(unsigned int v){ return (((v * 0x01008040U) | (v >> 3)) & 0x11111111U) % 15;}
Σnigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement