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?
You have it backwards. It is the binary number 1 shifted left by
x
spots.And that should make sense. If you want to check the 4th spot in the table, represented by
f
, you have to check thatf & 10000!=0
.You're right, shifting by one is very arbitrary and does not make sense.