/* This program's aim is to count the number of bits set in an integer */
#include<stdio.h>
int count=0;
int check(int);
int main()
{
int i,r;
char ch;
printf("enter the integer for which you want to check the bits set");
scanf("%d", &i);
r=check(i);
printf("the number of occurance of 1 in the integer is %d \n", r); /*don't know why isit printing 0 when i give 4 as input */
return 0;
}
int check(int j)
{
if((j & 1)==1)
count++;
for(int l=0;l<31;l++)
{
if( j>>1 & 1)
count++;
}
return count;
}
What is wrong with this program? Looks like some silly mistake or some conceptual one. Also do we need to write j>>1? can't we simply write j>>?