Links



Technical Discussion

Monday, November 06, 2006

 

Count the number of bits set in an unsigned integer.

/*
Program to count no. of bits in an unsigned integer
- C Language
*/

#include
#include
void main( void )
{
unsigned int a = 15;
int count = 0;

while( a )
{
++count;
a = a & ( a - 1 );
}

clrscr();
printf( "Count is %d\n", count );
getch();
}