代码
int second(int n, int m) {
int c = n ^ m;
int count = 0;
while (c)
{
c = c & (c - 1);
count++;
}
return count;
}
int main()
{
int n = 0;
int m = 0;
scanf("%d %d", &n, &m);
int ret=second(n, m);
printf("%d", ret);
return 0;
}
另一种写法
int main()
{
int n = 0;
int m = 0;
int i = 0;
scanf("%d %d", &n, &m);
int count = 0;
for (i = 0; i < 32; i++) {
if (((n >> i) & 1 )!= ((m >> i) & 1)) {
count++;
}
}
printf("%d", count);
return 0;
}