網(wǎng)站首頁 編程語言 正文
邏輯門是任何數(shù)字電路的基本構(gòu)建塊。它需要一兩個(gè)輸入并根據(jù)這些輸入產(chǎn)生輸出。輸出可能為高 (1) 或低 (0)。邏輯門使用二極管或晶體管實(shí)現(xiàn)。它也可以使用真空管、光學(xué)元件、分子等電磁元件構(gòu)成。在計(jì)算機(jī)中,大多數(shù)電子電路都是由邏輯門組成的。邏輯門用于執(zhí)行計(jì)算、數(shù)據(jù)存儲(chǔ)或展示面向?qū)ο缶幊蹋ㄓ绕涫抢^承的力量)的電路。?
定義了七個(gè)基本邏輯門:與門、或門、非門、與非門、或非門、異或門、異或門。?
1. 與門?
如果兩個(gè)輸入都為 1,與門的輸出為 1,否則為 0。
# 說明與門工作的 Python3 程序
def AND (a, b):
if a == 1 and b == 1:
return True
else:
return False
# 驅(qū)動(dòng)程序代碼
if __name__=='__main__':
print(AND(1, 1))
print("+---------------+----------------+")
print(" | AND Truth Table | Result |")
print(" A = False, B = False | A AND B =",AND(False,False)," | ")
print(" A = False, B = True | A AND B =",AND(False,True)," | ")
print(" A = True, B = False | A AND B =",AND(True,False)," | ")
print(" A = True, B = True | A AND B =",AND(True,True)," | ")
輸出:?
True
+---------------+----------------
?| AND Truth Table | ? ?Result |
?A = False, B = False | A AND B = False ?|?
?A = False, B = True ?| A AND B = False ?|?
?A = True, B = False ?| A AND B = False ?|?
?A = True, B = True ? | A AND B = True ? |?
2. 與非門?
如果兩個(gè)輸入都是 1,與非門(取反)輸出 0,否則輸出 1。
# 說明與非門工作的Python3程序
def NAND (a, b):
if a == 1 and b == 1:
return False
else:
return True
# 驅(qū)動(dòng)程序代碼
if __name__=='__main__':
print(NAND(1, 0))
print("+---------------+----------------+")
print(" | NAND Truth Table | Result |")
print(" A = False, B = False | A AND B =",NAND(False,False)," | ")
print(" A = False, B = True | A AND B =",NAND(False,True)," | ")
print(" A = True, B = False | A AND B =",NAND(True,False)," | ")
print(" A = True, B = True | A AND B =",NAND(True,True)," | ")
輸出:?
True
+---------------+----------------
?| NAND Truth Table | ? ?Result |
?A = False, B = False | A AND B = True ?|?
?A = False, B = True ?| A AND B = True ?|?
?A = True, B = False ?| A AND B = True ?|?
?A = True, B = True ? | A AND B = False |?
3. 或門?
如果兩個(gè)輸入中的任何一個(gè)為 1,或門的輸出為 1,否則為 0。
# Python3 程序來說明或門的工作
def OR(a, b):
if a == 1 or b ==1:
return True
else:
return False
# 驅(qū)動(dòng)程序代碼
if __name__=='__main__':
print(OR(0, 0))
print("+---------------+----------------+")
print(" | OR Truth Table | Result |")
print(" A = False, B = False | A OR B =",OR(False,False)," | ")
print(" A = False, B = True | A OR B =",OR(False,True)," | ")
print(" A = True, B = False | A OR B =",OR(True,False)," | ")
print(" A = True, B = True | A OR B =",OR(True,True)," | ")
輸出:?
False
+---------------+----------------+
?| OR Truth Table | ? ?Result |
?A = False, B = False | A OR B = False ?|?
?A = False, B = True ?| A OR B = True ? |?
?A = True, B = False ?| A OR B = True ? |?
?A = True, B = True ? | A OR B = True ? |?
4. 異或?
門 如果輸入中的任何一個(gè)不同,異或門的輸出為 1,如果它們相同,則輸出為 0。
# 說明異或門工作的 Python3 程序
def XOR (a, b):
if a != b:
return 1
else:
return 0
# 驅(qū)動(dòng)程序代碼
if __name__=='__main__':
print(XOR(5, 5))
print("+---------------+----------------+")
print(" | XOR Truth Table | Result |")
print(" A = False, B = False | A XOR B =",XOR(False,False)," | ")
print(" A = False, B = True | A XOR B =",XOR(False,True)," | ")
print(" A = True, B = False | A XOR B =",XOR(True,False)," | ")
print(" A = True, B = True | A XOR B =",XOR(True,True)," | ")
輸出:?
0
+---------------+----------------+
?| XOR Truth Table | Result |
?A = False, B = False | A XOR B = 0 ?|?
?A = False, B = True ?| A XOR B = 1 ?|?
?A = True, B = False ?| A XOR B = 1 ?|?
?A = True, B = True ? | A XOR B = 0 ?|?
5. NOT Gate?
它作為一個(gè)反相器。它只需要一個(gè)輸入。如果輸入為 1,它會(huì)將結(jié)果反轉(zhuǎn)為 0,反之亦然。
# Python3 程序來說明非門的工作原理
def NOT(a):
return not a
# 驅(qū)動(dòng)程序代碼
if __name__=='__main__':
print(NOT(0))
print("+---------------+----------------+")
print(" | NOT Truth Table | Result |")
print(" A = False | A NOT =",NOT(False)," | ")
print(" A = True, | A NOT =",NOT(True)," | ")
輸出:?
1
+---------------+----------------+
?| NOT Truth Table | Result |
?A = False | A NOT = 1 ?|?
?A = True, | A NOT = 0 ?|?
6. NOR 門?
NOR 門(取反的 OR)如果兩個(gè)輸入都為 0,則輸出為 1,否則為 0。
# Python3程序來說明或非門的工作
def NOR(a, b):
if(a == 0) and (b == 0):
return 1
elif(a == 0) and (b == 1):
return 0
elif(a == 1) and (b == 0):
return 0
elif(a == 1) and (b == 1):
return 0
# 驅(qū)動(dòng)程序代碼
if __name__=='__main__':
print(NOR(0, 0))
print("+---------------+----------------+")
print(" | NOR Truth Table | Result |")
print(" A = False, B = False | A NOR B =",NOR(False,False)," | ")
print(" A = False, B = True | A NOR B =",NOR(False,True)," | ")
print(" A = True, B = False | A NOR B =",NOR(True,False)," | ")
print(" A = True, B = True | A NOR B =",NOR(True,True)," | ")
輸出:?
1
+---------------+----------------+
?| NOT Truth Table | Result |
?A = False | A NOT = 1 ?|?
?A = True, | A NOT = 0 ?|?
7. XNOR 門?
XNOR 門(取反的 XOR)輸出 1,兩個(gè)輸入相同,如果兩者不同,則輸出 0。
# Python3 程序來說明非門的工作原理
def XNOR(a,b):
if(a == b):
return 1
else:
return 0
# 驅(qū)動(dòng)程序代碼
if __name__=='__main__':
print(XNOR(1,1))
print("+---------------+----------------+")
print(" | XNOR Truth Table | Result |")
print(" A = False, B = False | A XNOR B =",XNOR(False,False)," | ")
print(" A = False, B = True | A XNOR B =",XNOR(False,True)," | ")
print(" A = True, B = False | A XNOR B =",XNOR(True,False)," | ")
print(" A = True, B = True | A XNOR B =",XNOR(True,True)," | ")
輸出:?
1
+---------------+----------------+
?| XNOR Truth Table | ?Result |
?A = False, B = False | A XNOR B = 1 ?|?
?A = False, B = True ?| A XNOR B = 0 ?|?
?A = True, B = False ?| A XNOR B = 0 ?|?
?A = True, B = True ? | A XNOR B = 1 ?|?
原文鏈接:https://juejin.cn/post/7143877949953933349
相關(guān)推薦
- 2023-04-22 python的open函數(shù)使用案例代碼_python
- 2023-05-08 C語言中關(guān)于樹和二叉樹的相關(guān)概念_C 語言
- 2022-07-09 常見的dos命令
- 2023-05-29 Postgresql數(shù)據(jù)庫角色創(chuàng)建登錄詳解_PostgreSQL
- 2022-03-25 Mybatis聯(lián)合查詢的實(shí)現(xiàn)方法(多表聯(lián)合查詢)
- 2023-04-09 利用Matplotlib實(shí)現(xiàn)單畫布繪制多個(gè)子圖_python
- 2023-02-14 OKhttp攔截器實(shí)現(xiàn)實(shí)踐環(huán)節(jié)源碼解析_Android
- 2022-12-27 Qt實(shí)現(xiàn)UDP通信的示例代碼_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支