本文全面涵盖了Python中的不同逻辑端口。逻辑端口是实现数字组件的最基本材料。逻辑端口的使用范围从计算机架构到电子领域。
这些端口处理二进制值,无论是0或1。不同类型的端口需要不同的输入数字,但它们都提供一个单一的输出。
让我们尝试在Python语言中实现逻辑门。
Python 中的基本逻辑门户
电路开发中有三个最基本的逻辑门。
或门
此端口提供输出为 1 如果任何输入都是 1 它与对二进制数字的addition
_ 操作相似。
上面显示的表是真相表. 它用于显示所有输入值的组合到一个 ' OR' 端口. 表旁边的数字表示一个
' OR' 端口。
在Python中使用:
1# Function to simulate OR Gate
2def OR(A, B):
3 return A | B
4
5print("Output of 0 OR 0 is", OR(0, 0))
6print("Output of 0 OR 1 is", OR(0, 1))
7print("Output of 1 OR 0 is", OR(1, 0))
8print("Output of 1 OR 1 is", OR(1, 1))
我们得到以下的产出:
1Output of 0 OR 0 is 0
2Output of 0 OR 1 is 1
3Output of 1 OR 0 is 1
4Output of 1 OR 1 is 1
和门
此端口提供0的输出,如果任何输入均为0。
我们可以在真理表中看到,当两个输入中的任何一个为0时,输出也为0。
在Python中使用:
1# Function to simulate AND Gate
2def AND(A, B):
3 return A & B
4
5print("Output of 0 AND 0 is", AND(0, 0))
6print("Output of 0 AND 1 is", AND(0, 1))
7print("Output of 1 AND 0 is", AND(1, 0))
8print("Output of 1 AND 1 is", AND(1, 1))
输出:
1Output of 0 AND 0 is 0
2Output of 0 AND 1 is 0
3Output of 1 AND 0 is 0
4Output of 1 AND 1 is 1
不是门
此端口提供给定的输入的否定,此端口仅支持一个输入。
上面的表清楚地显示了比特的逆转,旁边的数字代表了NOT
门。
二进制 NOT 门的 Python 实现:
1# Function to simulate NOT Gate
2def NOT(A):
3 return ~A+2
4
5print("Output of NOT 0 is", NOT(0))
6print("Output of NOT 1 is", NOT(1))
输出:
1Output of NOT 0 is 1
2Output of NOT 1 is 0
注: 函数NOT()
为 0 和 1 位值提供正确的结果。
通用逻辑门在Python
有两个通用逻辑门,‘NAND’和‘NOR’,它们被命名为通用,因为任何布尔电路只能使用这些门来实现。
南京大门
NAND
端口是AND
端口的组合,其次是NOT
端口,与AND
端口相反,它只有在两个位数设置时提供0的输出,否则为1。
在Python中,NAND()
函数可以使用以前创建的AND()
和OR()
函数实现。
1# Function to simulate AND Gate
2def AND(A, B):
3 return A & B;
4
5# Function to simulate NOT Gate
6def NOT(A):
7 return ~A+2
8
9# Function to simulate NAND Gate
10def NAND(A, B):
11 return NOT(AND(A, B))
12
13print("Output of 0 NAND 0 is", NAND(0, 0))
14print("Output of 0 NAND 1 is", NAND(0, 1))
15print("Output of 1 NAND 0 is", NAND(1, 0))
16print("Output of 1 NAND 1 is", NAND(1, 1))
我们得到以下的产出:
1Output of 0 NAND 0 is 1
2Output of 0 NAND 1 is 1
3Output of 1 NAND 0 is 1
4Output of 1 NAND 1 is 0
也没有门
NOR
端口是由 OR
端口随后被 NOT
端口串联的结果,与 OR
端口不同,它提供 1 的输出,当所有输入为 0。
类似于NAND()
函数,NOR()
函数可以使用已经创建的函数实现。
1# Function to calculate OR Gate
2def OR(A, B):
3 return A | B;
4
5# Function to simulate NOT Gate
6def NOT(A):
7 return ~A+2
8
9# Function to simulate NOR Gate
10def NOR(A, B):
11 return NOT(OR(A, B))
12
13print("Output of 0 NOR 0 is", NOR(0, 0))
14print("Output of 0 NOR 1 is", NOR(0, 1))
15print("Output of 1 NOR 0 is", NOR(1, 0))
16print("Output of 1 NOR 1 is", NOR(1, 1))
输出:
1Output of 0 NOR 0 is 1
2Output of 0 NOR 1 is 0
3Output of 1 NOR 0 is 0
4Output of 1 NOR 1 is 0
Python 独家逻辑门户
有两种特殊类型的逻辑端口,XOR和XNOR,它们集中在0或1的输入数量上,而不是单个值。
XOR 门
Exclusive-OR 的缩写, `'XOR' 端口提供 1 的输出,当输入中的 1s 的数目是奇怪的。
在上面的表中,我们可以清楚地看到XOR门的输出,当输入中的数目为1时,它提供了1的输出,这很奇怪。
我们可以通过在Python中轻松实现XOR()
函数:
1# Function to simulate XOR Gate
2def XOR(A, B):
3 return A ^ B
4
5print("Output of 0 XOR 0 is", XOR(0, 0))
6print("Output of 0 XOR 1 is", XOR(0, 1))
7print("Output of 1 XOR 0 is", XOR(1, 0))
8print("Output of 1 XOR 1 is", XOR(1, 1))
我们得到以下的产出:
1Output of 0 XOR 0 is 0
2Output of 0 XOR 1 is 1
3Output of 1 XOR 0 is 1
4Output of 1 XOR 1 is 0
XNOR 门
它是由XOR
和NOT
端口的组合形成的,而不是XOR
,它提供了1的输出,当输入中的1s数是均匀的。
XNOR()
函数可以通过使用Python中的XOR()
和NOT()
函数来实现。
1# Function to simulate XOR Gate
2def XOR(A, B):
3 return A ^ B
4
5# Function to simulate NOT Gate
6def NOT(A):
7 return ~A+2
8
9# Function to simulate XNOR Gate
10def XNOR(A, B):
11 return NOT(XOR(A, B))
12
13print("Output of 0 XNOR 0 is", XNOR(0, 0))
14print("Output of 0 XNOR 1 is", XNOR(0, 1))
15print("Output of 1 XNOR 0 is", XNOR(1, 0))
16print("Output of 1 XNOR 1 is", XNOR(1, 1))
输出:
1Output of 0 XNOR 0 is 1
2Output of 0 XNOR 1 is 0
3Output of 1 XNOR 0 is 0
4Output of 1 XNOR 1 is 1
结论
作为一个程序员,你需要知道在Python中的逻辑门和操作员,我们希望这篇文章能让读者了解Python中的逻辑门的基本知识和执行。
要进一步阅读,请查看我们的其他Python教程( / 社区 / 教程 / Python)。