hamming distance 练习题

题目:The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.

想了半天没有想到什么有趣的解决方案,初步的想法是先转化成2进制,然后比较不同。
但是在讨论区看到了一种相当有趣的解法。记录一下。
Integer.bitCount(x ^ y);一行代码解决了问题,简洁。