Count number of bits to be flipped to convert A to B [Amazon]

Problem:

Count number of bits needed to be flipped to convert from integer A to B.

#include <stdio.h>

int main() {
	//code
	int t,a,b,c;
	int count=0;
	scanf("%d",&t);
	while(t--)
	{
	    scanf("%d%d",&a,&b);
	    c = a^b;
	    count=0;
	    while(c)
	    {
	        if(c&1)
	        count+=1;
	        c>>=1;
	    }
	    printf("%d\n",count);
	}
	return 0;
}

 

Comments are closed.