The last answer in this question shows that a binary truth table can be represented as a binary number:
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
Can be represented by 01010011
.
The entries in the table can also be evaluated using this number.
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
My question is about the evaluate
function provided by the answer. Why must we left shift the bits by one?