题目 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。 给出两个整数 x 和 y,计算它们之间的汉明距离。 注意 都是大于0的数。 思路 两个数直接异或,看二进制中1的个数。 代码 class Solution { public: int hammingDistance(int x, int y) { int res = 0; int temp = x ^ y; while ( temp ) { if ( ( temp & 1 ) == 1 ) ++res; temp >>= 1; } return res; } };