alternate implementation of CountBits

This commit is contained in:
Dan Buch 2011-06-19 18:38:51 -04:00
parent 0e86ed72e2
commit 74dfe433b9

View File

@ -0,0 +1,32 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
int get_bit_count(unsigned int x)
{
int count=0;
while(x)
{
count++;
x = x&(x-1);
}
return count;
}
int main()
{
int to_test[] = {0, 5, 7};
int current;
for (int i = 0; i < 3 ; i++) {
current = to_test[i];
printf("get_bit_count(%d) = %d\n", current, get_bit_count(current));
}
}
/* vim:filetype=c:fileencoding=utf-8
*/