Counting number of one's in a binay number

Saturday, May 3, 2014

i don't understand why this function is giving number of one's in the binary number.


for example



input: 7(111)
ouput: 3


the function is



scanf("%d", &a);
while(a)
{
oneina++;
a=a&(a-1);
}
printf("%d", oneina);






http://ift.tt/Sn2vgI