求两个数二进制中不同位的数
//求两个数二进制中不同位的数
//编程实现:两个int(32位)整数m和n的二进制表达中,
//有多少个位(bit)不同?
//输入例子 :1999 2299
//输出例子:7
#include<stdio.h>
int main()
{//第一步:输入一个整数int a = 0;printf("请输入一个整数:");scanf("%d", &a);//第二步:将这个整数(十进制)转换成二进制//15/2=7 7/2=3 3/2=1 1/2=0//15%2=1 7%2=1 3%2=1 1%2=1//一共有三十二位数,32-n//n是存入的个数//写一个数组,存入二进制数int b[32] = { 0 };int c[32] = { 0 };int j = 0;int i = 0;while (a){i = a % 2;b[j++] = i;a = a / 2;}//输出一个b数组看看for (i = 0; i < 32; i++){printf("%d ", b[i]);}printf("\n");int d = j - 1;//将二进制数,存入cfor (i = 32 - j; i < 32; i++){c[i] = b[d--];}//输出一个c看看for (i = 0; i < 32; i++){printf("%d ", c[i]);}printf("\n");int s = 0;printf("请输入一个整数:");scanf("%d", &s);int e[32] = { 0 };int f[32] = { 0 };j = 0;i = 0;while (s){i = s % 2;e[j++] = i;s = s / 2;}//输出一个e数组看看for (i = 0; i < 32; i++){printf("%d ", e[i]);}printf("\n");d = j - 1;//将二进制数,存入ffor (i = 32 - j; i < 32; i++){f[i] = e[d--];}//输出一个f看看for (i = 0; i < 32; i++){printf("%d ", f[i]);}printf("\n");d = 0;for (i = 0; i < 32; i++){if (c[i] == f[i])continue;elsed++;}printf("%d\n",d);return 0;}
输出结果: