邏輯閘的Python實現與簡單應用
阿新 • • 發佈:2021-12-17
邏輯閘的Python實現與簡單應用
基本的門
- 與非門
- 或非門
- elementgate.py
#coding=utf-8 def roc_nor(a, b): return int(not(a or b)) def roc_nand(a, b): return int(not(a and b)) if __name__ == "__main__": print("1與0與非:",roc_nand(1,0)) print("0與1與非:",roc_nand(0,1)) print("0與0與非:",roc_nand(0,0)) print("1與1與非:",roc_nand(1,1)) print("1與0或非:",roc_nor(1,0)) print("0與1或非:",roc_nor(0,1)) print("0與0或非:",roc_nor(0,0)) print("1與1或非:",roc_nor(1,1))
基礎的門(由與非門和或非門實現)
- 非門
- 與門
- 或門
- 異或門
- basicgate.py
#coding=utf-8 import elementgate def roc_not(a): return elementgate.roc_nand(a, a) def roc_and(a, b): return roc_not(elementgate.roc_nand(a,b)) def roc_or(a,b): return roc_not(elementgate.roc_nor(a,b)) def roc_xor(a,b): return roc_and(roc_or(a,b),elementgate.roc_nand(a,b)) if __name__=="__main__": print("1與0與非:",elementgate.roc_nand(1,0)) print("0與1與非:",elementgate.roc_nand(0,1)) print("0與0與非:",elementgate.roc_nand(0,0)) print("1與1與非:",elementgate.roc_nand(1,1)) print("1與0或非:",elementgate.roc_nor(1,0)) print("0與1或非:",elementgate.roc_nor(0,1)) print("0與0或非:",elementgate.roc_nor(0,0)) print("1與1或非:",elementgate.roc_nor(1,1)) print("0非:",roc_not(0)) print("1非:",roc_not(1)) print("1與0與門:",roc_and(1,0)) print("0與1與門:",roc_and(0,1)) print("0與0與門:",roc_and(0,0)) print("1與1與門:",roc_and(1,1)) print("1與0或門:",roc_or(1,0)) print("0與1或門:",roc_or(0,1)) print("0與0或門:",roc_or(0,0)) print("1與1或門:",roc_or(1,1)) print("1與0異或門:",roc_xor(1,0)) print("0與1異或門:",roc_xor(0,1)) print("0與0異或門:",roc_xor(0,0)) print("1與1異或門:",roc_xor(1,1))
邏輯閘的簡單實踐
- halfadder.py
- fulladder.py
- 多數表決電路.py
- 與投票一樣遵循少數服從多數,如0,1,0,結果為0
- 數值比較器.py
- 輸入兩個三位二進位制數,若結果相同,輸出為1,否則為0