True and True
True
not True or not False
True
not (True or False)
False
True ^ False
True
True ^ True
False
not (False or False)
True
fmt = lambda t : "X" if t else "."
def print_table(prop):
from itertools import product
print("A B C => P")
print("-" * 14)
for state in product([True, False], repeat=3):
args = ' '.join(fmt(s) for s in state)
res = prop(*state)
print("{} => {}".format(args, fmt(res)))
prop1 = lambda a, b, c : (a or (not a and b)) and c
print_table(prop1)
A B C => P -------------- X X X => X X X . => . X . X => X X . . => . . X X => X . X . => . . . X => . . . . => .
prop2 = lambda a, b, c : (a or b) and c
print_table(prop2)
A B C => P -------------- X X X => X X X . => . X . X => X X . . => . . X X => X . X . => . . . X => . . . . => .
prop3 = lambda a, b, c : a and (b ^ c)
print_table(prop3)
A B C => P -------------- X X X => . X X . => X X . X => X X . . => . . X X => . . X . => . . . X => . . . . => .
Die zwei Vektoren werden nach den üblichen Regeln addiert.
a = [True, True, False, True, True, False]
b = [True, True, False, False]
def boolean_add(a, b):
size = max(len(a), len(b))
# padding
for v in [a, b]:
while len(v) < size:
v.insert(0, False)
result = []
c = False
for pos in reversed(range(size)):
s = a[pos] ^ b[pos] ^ c
c = (a[pos] and b[pos]) or (c and (a[pos] ^ b[pos]))
result.insert(0, s)
result.insert(0, c)
return result
ab = boolean_add(a, b)
ab
[True, False, False, False, False, True, False]
def boolean_to_decimal(b):
result = 0
for i, x in enumerate(reversed(b)):
result += 2**i * x
return result
print boolean_to_decimal(a)
print boolean_to_decimal(b)
print boolean_to_decimal(ab)
54 12 66