150. Design a function to count how many numbers are set in a given input function.
#include<stdio.h>
int
set_bits(int);
int
num,r;
void
main()
{
printf("Enter a number : ");
scanf("%d",&num);
r=set_bits(num);
printf("%d",r);
printf("\n");
}
int
set_bits(int num)
{
int pos,c=0;
for(pos=0;pos<=31;pos++)
if((num>>pos&1)==1)
c++;
return
c;
}
Comments
Post a Comment