在循环的最后一次迭代中,i
是 11,但第 11 位x
已经是 0,所以结果是 0x4567。我不知道你为什么期待别的东西。在 的情况下,您在 的前一个值中x &= ~(1 << i)
清除了一点,而您继续为... 分配一个新值,一种情况是累积的,另一种不是。x
x_temp
x_temp
考虑两个循环的轨迹:
for `x &= ~(1 << i)`, you have
x is 0x4567 originally
x is 0x4467 after clearing 1<<8
x is 0x4467 after clearing 1<<9
x is 0x4067 after clearing 1<<10
x is 0x4067 after clearing 1<<11
但
for `x_temp = x & ~(1 << i)`, you have
x is 0x4567 (originally and forever)
x_temp is 0x4467 after clearing 1<<8 from x (which hasn't changed)
x_temp is 0x4567 after clearing 1<<9 from x (which hasn't changed)
x_temp is 0x4167 after clearing 1<<10 from x (which hasn't changed)
x_temp is 0x4567, after clearing 1<<11 from x (which hasn't changed)
也许这更清楚:假设 x = 5; 那么设置 x += 1 的循环将产生 6,7,8,9,10 的值,...但是设置 x_temp = x + 1 的循环将产生 6,6,6,6,6 的值, ...