此题来自 cracking the coding interview
write a function to determine the number of of bits required to convert integer A to integer B
input: 31, 14
output: 2
#include<stdio.h>
int count(int a, int b)
{
int c=0;
int x;
for(x=a^b; x!=0; x=x>>1)
c += x&1;
return c;
}
int main()
{
int a = 31;
int b = 14;
printf("%d\n",count(31,14));
}
关于位运算操作,经常会用到异或,xor