<分区>
所以我应该找到一个数字的奇偶校验,如果给定数字的二进制表示中有奇数个设置位,则奇偶校验为奇数,否则。我有一个解决方案,但代码不是很容易理解,因此我正在寻找一个解释。
int y = x ^ (x >> 1); //given number is x.
y = y ^ (y >> 2);
y = y ^ (y >> 4);
y = y ^ (y >> 8);
y = y ^ (y >> 16);
// Rightmost bit of y holds the parity value
// if (y&1) is 1 then parity is odd else even
if (y & 1)
return odd;
return even;