const unsigned char oneBits[] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
unsigned char CountOnes(unsigned char x)
{
unsigned char results;
results = oneBits[x&0x0f];
results += oneBits[x>>4];
return results
}
Have an array that knows the number of bits for 0 through 15. Add the results for each nibble.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…