Hello,

I have developed two version of code to find the number of 1s in a unsigned int. It has been told to do without using loops, so later version is of recirsion methog.

Can anybodu suggest better version to this..

Thanks

RT

Code:#include<stdio.h> void main(void) { unsigned int num, num_of_bits;// = 0; unsigned int mask = 0x1; // printf("0x%x\n",mask); printf("Please enter the number:"); scanf("%d",&num); /*while ( mask ) { if (num & mask) num_of_bits++; mask = mask<<1; }*/ /*while (num) { if ( num & mask ) num_of_bits++; num = num >> 1; }*/ /* for (; (num>0) && (num&mask); num = num>>1) num_of_bits++; printf("Number of bits = %d\n",num_of_bits);*/ num_of_bits = number_of_bits(num); printf("Number of bits = %d\n",num_of_bits); } unsigned int number_of_bits(unsigned int num) { static unsigned int num_bits = 0; if ( !num ) return num_bits; if ( num & 0x01 ) { number_of_bits(num = (num >> 1)); num_bits = num_bits+1; } else { number_of_bits(num = (num >> 1)); } return num_bits; }