Possible Duplicate:
Best algorithm to count the number of set bits in a 32-bit integer?
Hello,
Is there a more compact way of counting the number of ones in a byte without using a loop? I don't want to do the following if I don't have to. Thanks.
char myValue = 0x0F; int counter = 0; while (myValue > 0) { if (myValue & 0x01) { counter ++; } myValue = myValue >> 1; }
std::bitsetand then call thebitset::countfunction. In C, that doesn't exist.