试图理解为什么这两个功能不等价

Trying to understand why these two functions are not equivalent

我正在研究 javascript 中的一些位移,并试图理解为什么这两个位移函数不等价。目的是统计一个字节中set位的个数。

// correct output
let x = 13;
for (var c = 0; x; x >>= 1) {
  c += x & 1;
}
// outputs: c === 3

并且:

//incorrect output
let y = 13;
var b = 0;

for (let i = 0; i < 4; i++) {
  y >>= 1;
  b += y & 1;
}
// outputs: b === 2

不正确的版本在 y >>= 1 中的初始 y 中删除了 right-most 位,然后 将位添加到 b (所以它恰好给出了正确的结果当 y 为 12 时)。