Encoders
Hub for Computer Whizzes Register to join us

Encoders > More... > C > Program

/* Function bitcount(x) that counts the number of 1 bits in a number. */

#include <stdio.h>

main()
{
        printf("%d\n",bitcount(123));
}

int bitcount(unsigned x)
{
	int i;
        for(i=0;x;x>>=1)
                if(x&1)
			i++;
	return i;
}