Using Bitwise Operator Write A C Program To Count The Number Of 1's In The Binary Reprasentation Of An Integer
Program Code:
int main()
{
unsigned int n;
int count=0;
printf("Enter the value of n\n");
scanf("%d",&n);
while(n!=0)
{
if((n&1)==1)
count++;
n=n>>1;
}
printf("number of 1's are:%d\n",count);
return 0;
}
Input:
Enter the value of n
12
Output:
number of 1's are:
2
No comments:
Post a Comment