求出两个int(32位)整数m和n的二进制表达中,有多少bit位不同

#include<stdio.h>

#define MAX(X,Y) ((X)<(Y)?(X):(Y))

int main()

{

int a = 1999;

int b = 2299;

int num = 1;

int nam;

if (b % 2 == 1)

{

nam = 1;

}

else

{

nam = 0;

}

do

{


int A = a % 2;

int B = b % 2;

if (A != B)

{

num++;

}

else

{

num = num;

}

a = a / 2;

b = b / 2;

}while (b!=nam);

printf("%d", num);

}

方法二;

#include<stdio.h>

#define MAX(X,Y) ((X)>(Y)?(X):(Y))

int num_sing(int max)

{

if (max % 2 == 0)

{

return 0;

}

else

{

return 1;

}

}

int main()

{

int a = 1999;//1010 

int b = 2299;//1111

int i = 0;

int num = 0;

int max = MAX(a, b);

int sing = num_sing(MAX(a, b));

int cishu =0+sing;

  

while (max!= sing)

{

max = max / 2;

cishu++;

}

for (i = 0; i < cishu; i++)

{

if ((a&1)!=(b&1))

{

num++;

}

a = a >> 1;

b = b >> 1;

}

printf("%d", num);

}


你可能感兴趣的:(1.编程实现,有多少bit位不同)