Hi folks,
I am trying to find a more efficient way to count "How many bits are
'1' in an integer variable?".
I still have no idea to count the bits except using a loop and "if"
statements.
Could you know any other more efficient way?
Cuthbert
Code:int main (void) { int var = 0xFF0F; int i, count = 0; int mask = 1; for ( i = 0; i < sizeof(int)*8 ; i++ ) if ( mask<<i & var) count++ ; printf("%d\n", count); return 0; }