我不明白如何移动 16 位、8 位等。与'&'的组合检测偶数位中的1

I don't understand how shifting 16 bits,8 bits,etc. with the combination of the '&' detects 1s in the even bits

我正在编写一个程序来检测偶数位中是否有。例如 0101 在偶数处有一个。这个解决方案恰好有效,但我不知道为什么。

让我感到困惑的是,当我们向右移动 16 位时。例如对于 0101 我们只是创建 0000 0000 0000 0000 0101 对吗?然后我们用原始数字做一个 and ,所以它是 0000 0000 0000 0000 0101 & 0000 0000 0000 0000 0101 是同一个数字吗?因此,如果我们一遍又一遍地进行这种移动,最终 x&1 我看不出这对 return 1 if allEvenbits.

有何帮助
/* 
 * allEvenBits - return 1 if all even-numbered bits in word set to 1
 *   Examples allEvenBits(0xFFFFFFFE) = 0, allEvenBits(0x55555555) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 12
 *   Rating: 2
 */

int allEvenBits(int x) {
  //0xAA - OxFF are valid entries
  x = x & (x >> 16);
  x = x & (x >> 8);
  x = x & (x >> 4);
  x = x & (x >> 2);
  return x&1; 
}

请记住整数(至少对于本练习而言)是 32 位长。所以如果你右移 16 位:

0000 0000 0000 0000 0000 0000 0000 0101
------------------- -------------------
  These  \          Right bits discarded
  bits are  \
  shifted 16   \
  positions right \ 
                     \
                        \
                           \
                    -------------------
0000 0000 0000 0000 0000 0000 0000 0000
Zeros enter on left

其余班次继续这个过程;总的来说,总共移位了30位,所以只有最后两位可以有非0的值。只有偶数位置的所有位都为1,最后一位才会为1;如果奇数位置的所有位都为 1,则倒数第二位为 1 pnly。

此时合取为0,表示偶数位和奇数位均未设置。没错;只设置了两个偶数位。