为什么在将真值表评估为二进制数时进行位移?

Why bitshift when evaluating truth tables as binary numbers?

this question中的最后一个答案表明二进制真理table可以表示为二进制数:

0 0 0  | 1
0 0 1  | 1
0 1 0  | 0
0 1 1  | 0
1 0 0  | 1
1 0 1  | 0 
1 1 0  | 1
1 1 1  | 0

可以表示为01010011

table 中的条目也可以使用此数字进行评估。

def evaluate(f, x):
    return (f & (1<<x)) != 0

f = int('01010011',2)

>>> evaluate(f,int('100',2))
True
>>> evaluate(f,int('101',2))
False

我的问题是关于答案提供的evaluate功能。为什么我们必须将位左移一位?

你搞反了。它是二进制数 1 左移 x 点。

这应该是有道理的。如果要检查 table 中的第 4 个位置,由 f 表示,则必须检查 f & 10000!=0

你说得对,移动一位是非常武断的,没有意义。