Enter two integers m and nto calculate how many bits in the binary representation of m need to bechanged to get n?
Solution: The first step is to find the XOR operation of the two numbers, save the results of the XOR operation, and the second step counts the 1 digits of The result of the operation.
Program:
#include <stdio.h>
int count (int m,int N)
{
int t,count=0;
t = m^n;
while (t)
{
count++;
t=t& (t-1);
}
return count;
}
int main ()
{
int num1,num2,ret=0;
printf ("Please enter two integers:");
scanf ("%d,%d", &num1,&num2);
ret = count (num1,num2);
printf ("%d bits in the binary representation of change%d to get%d\n", num1,ret,num2);
return 0;
}
Results:
Please enter two integers : 10,13
Change the 3 bits in the binary representation to get
Please press any key to continue ...
This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1742006
C language: Enter two integers m and n, calculate how many bits in the binary representation of M need to be changed to get n