alternate implementation of CountBits
This commit is contained in:
parent
0e86ed72e2
commit
74dfe433b9
32
gowrikumar/src/12-countbits2.c
Normal file
32
gowrikumar/src/12-countbits2.c
Normal 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
|
||||||
|
*/
|
Loading…
Reference in New Issue
Block a user