When the integer is expected to be dense, you have the corresponding trick
size_t count = sizeof(x) * 8;
while(x != -1) {
x |= x+1;
--count;
}
return count;