1 class Solution 2 { 3 public: 4 int hammingDistance(int x, int y) 5 { 6 int z=x^y; 7 int count=0; 8 while(z) 9 { 10 count+=(z&1); 11 z>>=1; 12 } 13 return count; 14 } 15 };
异或一下,统计结果中为1的位数