#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int num1 = 0; int num2 = 0; int Temp = 0; int count = 0; printf("请输入两个数来比较他们二进制码不同位的个数:\n"); scanf("%d %d", &num1, &num2); Temp = num1 ^ num2; while (Temp != 0) { count++; Temp = Temp & (Temp - 1); } printf("二进制码不同位的个数为:%d\n", count); return 0; }
int Compare(int n, int m)
{
int a = n ^ m;
for (int i = 0; i < 32; i++)
{
if (((a>>i)&1)==1)
{
count++;
}
}
return count;
}
int main()
{
int n = 0;
int m = 0;
scanf("%d %d", &n, &m);
int ret=Compare(n, m);
printf("%d\n", ret);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> //求两个数二进制中不同位的个数 //按位异或相同为0,不同为1 int main() { int a = 0; int b = 0; scanf("%d %d", &a, &b); int ret = a ^ b;//将a和b按位异或的值存入ret中 int count = 0; for (int i = 0; i <32; i++) { if (((ret>>i) & 1) == 1)//数出ret中有多少个1则可以得出a和b二进制中有多少位不同 count++; } printf("%d\n",count); return 0; }
#include<stdio.h> int main() { int m = 0; int n = 0; int count = 0; scanf("%d %d", &m, &n); for (int i = 0; i <= 31; i++) { if (((m>>i) & 1)^((n>>i) & 1)) count++; } printf("%d", count); return 0; }
#include <stdio.h> int calc_diff_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,n; while(scanf("%d %d", &m, &n) == 2) { printf("%d\n", calc_diff_bit(m, n)); } return 0; }