Python中的逻辑门–入门教程

时间:2020-02-23 14:42:19  来源:igfitidea点击:

本文全面介绍了Python中的不同逻辑门。
逻辑门是实现数字组件的最基本材料。
逻辑门的使用范围从计算机体系结构到电子领域。

这些门处理0或者1的二进制值。
不同类型的门采用不同数量的输入,但它们全部提供单个输出。
这些逻辑门组合在一起会形成复杂的电路。

让我们尝试用Python语言实现逻辑门。

Python中的基本逻辑门

电路开发中有三个最基本的逻辑门。

或者门

如果输入中的任意一个为1,则此门输出为1。
就二进制数而言,此门类似于"加法"运算。

或者门

上面显示的表是真值表。
它用于展示" OR"门输入的所有值组合。
表格旁边的数字表示"或者"门。

用Python实现:

# Function to simulate OR Gate
def OR(A, B):
	return A | B	

print("Output of 0 OR 0 is", OR(0, 0))
print("Output of 0 OR 1 is", OR(0, 1))
print("Output of 1 OR 0 is", OR(1, 0))
print("Output of 1 OR 1 is", OR(1, 1))	

我们得到以下输出:

Output of 0 OR 0 is 0
Output of 0 OR 1 is 1
Output of 1 OR 0 is 1
Output of 1 OR 1 is 1

与门

如果两个输入中的任何一个为0,则此门的输出为0。
此操作被视为二进制数的乘法。

与门

我们可以在真值表中看到,只要两个输入中的任何一个为0,输出也为0。
旁边的数字表示"与"门。

用Python实现:

# Function to simulate AND Gate
def AND(A, B):
	return A & B	

print("Output of 0 AND 0 is", AND(0, 0))
print("Output of 0 AND 1 is", AND(0, 1))
print("Output of 1 AND 0 is", AND(1, 0))
print("Output of 1 AND 1 is", AND(1, 1))	

输出:

Output of 0 AND 0 is 0
Output of 0 AND 1 is 0
Output of 1 AND 0 is 0
Output of 1 AND 1 is 1

非门

此门提供给定输入的求反。
该门仅支持单个输入。

非门

上表清楚地显示了位的反转。
相邻的图表示" NOT"门。

二进制NOT Gate的Python实现:

# Function to simulate NOT Gate
def NOT(A):
	return ~A+2	

print("Output of NOT 0 is", NOT(0))
print("Output of NOT 1 is", NOT(1))

输出:

Output of NOT 0 is 1
Output of NOT 1 is 0

<p>Note: The <code>'NOT()' function provides correct results for bit values 0 and 1.</p>

Python中的通用逻辑门

有两个通用逻辑门," NAND"和" NOR"。
之所以将它们命名为Universal,是因为仅可使用这些门来实现任何布尔电路。

与非门

"与非"门是"与"门和"非"门的组合。
与" AND"门相反,仅当两个位置1时,它提供0输出,否则为1。

与非门

在Python中,可以使用之前创建的'AND()'和'OR()'函数来实现'NAND()'函数。

# Function to simulate AND Gate
def AND(A, B):
	return A & B;	

# Function to simulate NOT Gate
def NOT(A):
	return ~A+2	

# Function to simulate NAND Gate
def NAND(A, B):
	return NOT(AND(A, B))

print("Output of 0 NAND 0 is", NAND(0, 0))
print("Output of 0 NAND 1 is", NAND(0, 1))
print("Output of 1 NAND 0 is", NAND(1, 0))
print("Output of 1 NAND 1 is", NAND(1, 1))	

我们得到以下输出:

Output of 0 NAND 0 is 1
Output of 0 NAND 1 is 1
Output of 1 NAND 0 is 1
Output of 1 NAND 1 is 0

或者非门

" NOR"门是" OR"门与" NOT"门级联的结果。
与" OR"门相反,当所有输入均为0时,它提供的输出为1。

或者非门

与" NAND()"函数相似," NOR()"可以使用已经创建的函数来实现。

# Function to calculate OR Gate
def OR(A, B):
	return A | B;	

# Function to simulate NOT Gate
def NOT(A):
	return ~A+2	

# Function to simulate NOR Gate
def NOR(A, B):
	return NOT(OR(A, B))

print("Output of 0 NOR 0 is", NOR(0, 0))
print("Output of 0 NOR 1 is", NOR(0, 1))
print("Output of 1 NOR 0 is", NOR(1, 0))
print("Output of 1 NOR 1 is", NOR(1, 1))	

输出:

Output of 0 NOR 0 is 1
Output of 0 NOR 1 is 0
Output of 1 NOR 0 is 0
Output of 1 NOR 1 is 0

Python中的独家逻辑门

逻辑门有两种特殊类型,即XOR和XNOR,它们专注于0或者1的输入数量,而不是单个值。

异或者门

当输入中的1的数量为奇数时," XOR"门的缩写为"异或者"。

异或者门

我们可以在上表中清楚地看到XOR门的输出。
当输入中的数字为1时,它提供的输出为1,这是奇数。

我们可以通过以下方式在Python中轻松实现`XOR()'函数:

# Function to simulate XOR Gate
def XOR(A, B):
	return A ^ B

print("Output of 0 XOR 0 is", XOR(0, 0))
print("Output of 0 XOR 1 is", XOR(0, 1))
print("Output of 1 XOR 0 is", XOR(1, 0))
print("Output of 1 XOR 1 is", XOR(1, 1))	

我们得到以下输出:

Output of 0 XOR 0 is 0
Output of 0 XOR 1 is 1
Output of 1 XOR 0 is 1
Output of 1 XOR 1 is 0

异或者门

它是由" XOR"和" NOT"门的组合形成的。
与" XOR"相反,当输入中1的数量为偶数时,它提供1的输出。

异或者门

可以通过在Python中使用" XOR()"和" NOT()"函数来实现" XNOR()"函数。

# Function to simulate XOR Gate
def XOR(A, B):
	return A ^ B

# Function to simulate NOT Gate
def NOT(A):
	return ~A+2	

# Function to simulate XNOR Gate
def XNOR(A, B):
	return NOT(XOR(A, B))

print("Output of 0 XNOR 0 is", XNOR(0, 0))
print("Output of 0 XNOR 1 is", XNOR(0, 1))
print("Output of 1 XNOR 0 is", XNOR(1, 0))
print("Output of 1 XNOR 1 is", XNOR(1, 1))	

输出:

Output of 0 XNOR 0 is 1
Output of 0 XNOR 1 is 0
Output of 1 XNOR 0 is 0
Output of 1 XNOR 1 is 1