Programming realization: How many bits are different in the binary representation of two int (32-bit) integers m and n.
For example: input 1 and 2, output 2. The
code is as follows:
#include<stdio.h>
int bit(int m,int n)
{
int tmp=m^n;
int count=0;
while(tmp)
{
tmp=tmp&(tmp-1);
count++;
}
return count;
}
int main()
{
int m=0;
int n=0;
int count;
printf("请输入您要求的两个数:\n");
scanf("%d %d",&m,&n);
count=bit(m,n);
printf("m,n的二进制表达中,有%d个位不同\n",count);
return 0;
}
The result of the operation is shown in the figure:
Caicai's code, I hope it can help you!