All pastes #53687 Raw Edit

Something

public text v1 · immutable
#53687 ·published 2006-05-05 09:16 UTC
rendered paste body
#include <stdio.h>

char val[7];
int bits = 0;

int bit_count(int test_num,int i){
if (i > 15)
	return(0);
else{
	if (((0x8000 >> i) & test_num) != 0){
		printf("Checked bit %d, and the answer is yes. +1\n",i);
		return(1 + bit_count(test_num,i + 1));
	}
	else{
		printf("Checked bit %d, and the answer is no.  +0\n",i);
		return(0 + bit_count(test_num,i + 1));
	} 	
}	
}

int main()
{
while(1){
int count_me;
printf("Gimme an integer.\n");
fgets(val,sizeof(val),stdin);
sscanf(val,"%d",&count_me);

printf("There are %d bits set in the number %d.\n",bit_count(count_me,bits),count_me);
bits = 0;
}
return (0);
}