逻辑门是集成电路上的基本组件,可实现更为复杂的逻辑运算,在程序中能应对多变量下的多种情况。
PS:如果状态各不相同,使用枚举更为合适
类型 | 又名 | 短释 | 逻辑函数表示 |
---|---|---|---|
NOT | 非门 | 逆转输入 | A’ |
AND | 与门 | 全 True 时才输出 True | A*B |
NAND | 与非门 | 全 True 时才输出 False | (A*B)’ |
OR | 或门 | 全 False 时才输出 False | A + B |
NOR | 或非门 | 全 False 时才输出 True | (A + B)’ |
XOR | 异或门 | 输入相同时为 False,否则为 True | A’*B + A*B’ |
XNOR | 同或门 | 输入相同时为 True,否则为 False | A*B + A’*B’ |
A和B为输入变量
def NOT(a):
'''非门'''
return not a
def AND(a, b):
'''与门'''
return a and b
def NAND(a, b):
'''与非门'''
return not (a and b)
def OR(a, b):
'''或门'''
return a or b
def NOR(a, b):
'''或非门'''
return not (a or b)
def XOR(a, b):
'''异或门'''
return not (a and b) and not (not a and not b)
# if a and b:
# return False
# elif not a and not b:
# return False
# else:
# return True
def XNOR(a, b):
'''同或门'''
return (a and b) or (not a and not b)
# if a and b:
# return True
# elif not a and not b:
# return True
# else:
# return False
if __name__ == '__main__':
# NOT 非门 逆转输入
assert NOT(0) == True
assert NOT(1) == False
# AND 与门 全True时才输出True
assert AND(0, 0) == False
assert AND(0, 1) == False
assert AND(1, 0) == False
assert AND(1, 1) == True
# NAND 与非门 全True时才输出False
assert NAND(0, 0) == True
assert NAND(0, 1) == True
assert NAND(1, 0) == True
assert NAND(1, 1) == False
# OR 或门 全False时才输出False
assert OR(0, 0) == False
assert OR(0, 1) == True
assert OR(1, 0) == True
assert OR(1, 1) == True
# NOR 或非门 全False时才输出True
assert NOR(0, 0) == True
assert NOR(0, 1) == False
assert NOR(1, 0) == False
assert NOR(1, 1) == False
# XOR 异或门 输入相同时为False,否则为True
assert XOR(0, 0) == False
assert XOR(0, 1) == True
assert XOR(1, 0) == True
assert XOR(1, 1) == False
# XNOR 同或门 输入相同时为True,否则为False
assert XNOR(0, 0) == True
assert XNOR(0, 1) == False
assert XNOR(1, 0) == False
assert XNOR(1, 1) == True
此代码变量输入不仅适用于纯01,更是广义的判断。
例如XOR异或门,纯01使用 a ^ b
即可,广义的则包含了 Python 自带的判断机制,如 XOR(0, [], None)
为 False,XOR(0, [], True)
为 True
from operator import truth
def NOT(a):
'''非门,逆转输入
>>> NOT(0)
True
>>> NOT(1)
False
>>> NOT([])
True
>>> NOT([1])
False
'''
return not a
def AND(*args):
'''与门,全True时才输出True
>>> AND(0, 0)
False
>>> AND(0, 1)
False
>>> AND(1, 0)
False
>>> AND(1, 1)
True
>>> AND(1, [1], None)
False
>>> AND(1, [1], True)
True
'''
expression = ' and '.join(['{}'.format(k) for k in args])
expression = 'truth({})'.format(expression)
return eval(expression)
def NAND(*args):
'''与非门,全True时才输出False
>>> NAND(0, 0)
True
>>> NAND(0, 1)
True
>>> NAND(1, 0)
True
>>> NAND(1, 1)
False
>>> NAND(1, [1], None)
True
>>> NAND(1, [1], True)
False
'''
expression = ' and '.join(['{}'.format(k) for k in args])
expression = 'truth(not ({}))'.format(expression)
return eval(expression)
def OR(*args):
'''或门,全False时才输出False
>>> OR(0, 0)
False
>>> OR(0, 1)
True
>>> OR(1, 0)
True
>>> OR(1, 1)
True
>>> OR(0, [], None)
False
>>> OR(0, [], True)
True
'''
expression = ' or '.join(['{}'.format(k) for k in args])
expression = 'truth({})'.format(expression)
return eval(expression)
def NOR(*args):
'''或非门,全False时才输出True
>>> NOR(0, 0)
True
>>> NOR(0, 1)
False
>>> NOR(1, 0)
False
>>> NOR(1, 1)
False
>>> NOR(0, [], None)
True
>>> NOR(0, [], True)
False
'''
expression = ' or '.join(['{}'.format(k) for k in args])
expression = 'truth(not ({}))'.format(expression)
return eval(expression)
def XOR(*args):
'''异或门,输入相同时为False,否则为True
>>> XOR(0, 0)
False
>>> XOR(0, 1)
True
>>> XOR(1, 0)
True
>>> XOR(1, 1)
False
>>> XOR(0, [], None)
False
>>> XOR(0, [], True)
True
'''
e1 = ' and '.join(['{}'.format(k) for k in args])
e2 = ' and '.join(['not {}'.format(k) for k in args])
expression = 'truth(not ({}) and not ({}))'.format(e1, e2)
return eval(expression)
def XNOR(*args):
'''同或门,输入相同时为True,否则为False
>>> XNOR(0, 0)
True
>>> XNOR(0, 1)
False
>>> XNOR(1, 0)
False
>>> XNOR(1, 1)
True
>>> XNOR(0, [], None)
True
>>> XNOR(0, [], True)
False
'''
e1 = ' and '.join(['{}'.format(k) for k in args])
e2 = ' and '.join(['not {}'.format(k) for k in args])
expression = 'truth(({}) or ({}))'.format(e1, e2)
return eval(expression)