Program to count number of bits are set to 1 in an integer in C
#include"stdio.h"
main()
{
int x,n;
printf("Enter a number :");
scanf("%d",&n);
x=bit_count(n);
printf("%d ",x);
}
int bit_count(int n)
{
int c=0;
while(n>0)
{
c++;
n=n&n-1;
}
return c;
}
main()
{
int x,n;
printf("Enter a number :");
scanf("%d",&n);
x=bit_count(n);
printf("%d ",x);
}
int bit_count(int n)
{
int c=0;
while(n>0)
{
c++;
n=n&n-1;
}
return c;
}
No comments
Post a Comment