#461 Hamming Distance

2017-04-27  本文已影响0人  乐正君Krizma

题目:

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.



代码:

/**

* @param {number} x

* @param {number} y

* @return {number}

*/

var hammingDistance = function(x, y) {

var n = 0, s = x ^ y;

for(n = 0; n < s; n++){

s &= (s - 1)

}

return n;

};

上一篇 下一篇

猜你喜欢

热点阅读