Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

461. Hamming Distance #160

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

就是位数不同的总数,所以先做^异或运算,再for循环,做移位运算,再做和1做&与 public class Solution { public int hammingDistance(int x, int y) { int xory = x ^ y, count = 0; for(int i = 0; i < 32; i++) { count += (xory >> i) & 1; } return count; } }

Shawngbk commented 7 years ago

FB