C语言实现两个int(32位)整数m和n的二进制表达中找不同位

时间:2023-01-11 23:39:06

VS2013

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
    int i = 0, count=0;
    int num1 = 0, num2 = 0;
    printf("请输入两个整数:\n");
    scanf("%d%d", &num1, &num2);
    int a[32] = { 0 };
    int b[32] = { 0 };
    for (i = 0; i < 32; i++)
    {
        a[i] = num1 % 2;
        b[i] = num2 % 2;
        if (a[i]!=b[i])
        {
            count++;
        }
        num1 = num1 / 2;
        num2 = num2 / 2;
    }
    printf("不同的数有%d个:", count);
    system("pause");
    return 0;
}