box-o-sand/oldstuff/PracticingC/gowrikumar/src/12-countbits2.c

33 lines
467 B
C
Raw Normal View History

2011-06-19 22:38:51 +00:00
/**
* :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
*/