布尔代数基础与四种基本逻辑门
结合门禁类比与布尔/香农理论渊源,系统讲解 AND、OR、NOT、XOR 四种基本逻辑运算的真值表、标准逻辑门图形符号、德摩根定律以及交互与代码验证方法。
学完这一章,你能够
- 结合门禁类比与布尔/香农理论渊源,系统讲解 AND、OR、NOT、XOR 四种基本逻辑运算的真值表、标准逻辑门图形符号、德摩根定律以及交互与代码验证方法。
门禁类比与布尔代数的理论奠基
布尔代数是所有数字集成电路的数学底座,仅凭四种基本逻辑运算的组合就能搭出任意复杂的处理器电路。用建筑安防场景即可秒懂它们的行为:AND(与)如同双钥匙金库门,必须两把钥匙同时转动才开锁;OR(或)如同前后双门办公楼,只要任意一扇门打开就能通行;NOT(非)如同门外“空闲/使用中”翻转牌,按一下按钮状态即刻反转;XOR(异或)则类似要求两盏灯必须一开一关的互锁警报,状态不同输出 1、相同时输出 0。
这套代数体系由英国数学家乔治·布尔(George Boole)于 1854 年在《思维定律的研究》中创立,首次将人类逻辑推理转化为严谨的真/假符号演算。1938 年,克劳德·香农(Claude Shannon)在 MIT 硕士论文中开创性地证明:布尔代数可直接用来描述和化简继电器开关网络。正是这一跨界桥梁催生了整个数字电路工程,现代芯片内部每秒都在执行数以亿计的布尔运算。
四种基本逻辑运算的表达式与真值表
AND 与运算遵循“全 1 才 1”:仅当所有输入均为 1 时输出才为 1,犹如串联开关,逻辑表达式写作 `Y = A · B`(或简写 `Y = AB`)。OR 或运算遵循“有 1 就 1”:只要至少一个输入为 1 则输出为 1,犹如并联开关,逻辑表达式写作 `Y = A + B`(切记这是逻辑或而非算术加,布尔代数中 `1 + 1 = 1`)。两者的完整真值表见下表。
NOT 非运算是唯一的一元基本运算,口诀为“0 变 1,1 变 0”,表达式记作 `Y = A'` 或 `NOT A`,电路中常在引脚端画小圆圈代表反相。XOR 异或运算遵循“不同为 1、相同为 0”,可用前三种基本门展开为 `A XOR B = (A' · B) + (A · B')`,它在二进制加法器、奇偶校验与流密码加密中扮演关键角色。
标准逻辑门图形符号与交互演示验证
工程原理图为四种基本门制定了高度可辨的标准图形符号:AND 门呈直线底边接半圆弧的 D 形;OR 门呈内收弧线汇聚于尖端的盾形;NOT 门(反相器)是在缓冲三角形顶端衔接一个小圆圈;XOR 门则在 OR 门输入侧多加一道平行弧线以示区分。无论 CPU 内部的运算器或控制器多么庞大,归根结底都可逐级拆解为这些基础逻辑门的互联组合。
将四类逻辑门的标准 SVG 符号与多输出联合真值表对照呈现,并切换输入 A、B 的 0/1 状态,可以同步观察 AND、OR、NOT A、NOT B 与 XOR 的实时联动输出(汇总见下表)。在 Python 中,通过定义基础逻辑函数遍历 `(0, 0)` 至 `(1, 1)` 四组输入,不仅能自动生成完整真值表,还能穷举验证两条德摩根定律:`NOT(A AND B) == (NOT A) OR (NOT B)` 以及 `NOT(A OR B) == (NOT A) AND (NOT B)`。
AND 与运算真值表
| A | B | A AND B |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
OR 或运算真值表
| A | B | A OR B |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
NOT 非运算真值表
| A | NOT A |
| 0 | 1 |
| 1 | 0 |
XOR 异或运算真值表
| A | B | A XOR B |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
四种基本逻辑门联合真值表
| A | B | AND | OR | NOT A | NOT B | XOR |
| 0 | 0 | 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |
逻辑门表达式与交互状态输出速览(以 A=0, B=1 为例)
| 逻辑门 | 逻辑表达式 | 示例输出(A=0, B=1) | 记忆口诀 |
| AND 与门 | A AND B | 0 | 全 1 才 1 |
| OR 或门 | A OR B | 1 | 有 1 就 1 |
| NOT 非门(A) | NOT A | 1 | 0 变 1,1 变 0 |
| NOT 非门(B) | NOT B | 0 | 0 变 1,1 变 0 |
| XOR 异或门 | A XOR B | 1 | 不同为 1 |
离线穷举四大基本逻辑门真值表、德摩根定律与生活门禁场景映射
展开查看收起
"""Offline Computer Organization lab for Chapter 07 (boolean-algebra-logic-gates)."""
import json
VERSION = "CPython 3.12.14 offline computer organization simulation verifier"
def AND(a: int, b: int) -> int:
return 1 if (a == 1 and b == 1) else 0
def OR(a: int, b: int) -> int:
return 1 if (a == 1 or b == 1) else 0
def NOT(a: int) -> int:
return 1 if a == 0 else 0
def XOR(a: int, b: int) -> int:
return 1 if a != b else 0
def case_basic_gates_truth_tables():
pairs = [(0, 0), (0, 1), (1, 0), (1, 1)]
and_tt = [AND(a, b) for a, b in pairs]
or_tt = [OR(a, b) for a, b in pairs]
not_tt = [NOT(a) for a in (0, 1)]
xor_tt = [XOR(a, b) for a, b in pairs]
xor_from_basic = [OR(AND(NOT(a), b), AND(a, NOT(b))) for a, b in pairs]
actual = {
"AND": and_tt,
"OR": or_tt,
"NOT": not_tt,
"XOR": xor_tt,
"xor_composite_matches": xor_tt == xor_from_basic,
}
expected = {
"AND": [0, 0, 0, 1],
"OR": [0, 1, 1, 1],
"NOT": [1, 0],
"XOR": [0, 1, 1, 0],
"xor_composite_matches": True,
}
return {
"name": "basic_gates_truth_tables",
"summary": "穷举校验 AND/OR/NOT/XOR 四种基本逻辑门真值表及 XOR=(A'·B)+(A·B') 组合等价性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_demorgan_laws_exhaustive():
pairs = [(0, 0), (0, 1), (1, 0), (1, 1)]
law1_ok = all(NOT(AND(a, b)) == OR(NOT(a), NOT(b)) for a, b in pairs)
law2_ok = all(NOT(OR(a, b)) == AND(NOT(a), NOT(b)) for a, b in pairs)
details = [
{
"a": a,
"b": b,
"law1": (NOT(AND(a, b)), OR(NOT(a), NOT(b))),
"law2": (NOT(OR(a, b)), AND(NOT(a), NOT(b))),
}
for a, b in pairs
]
actual = {"law1_all_hold": law1_ok, "law2_all_hold": law2_ok, "cases": details}
expected = {
"law1_all_hold": True,
"law2_all_hold": True,
"cases": [
{"a": 0, "b": 0, "law1": (1, 1), "law2": (1, 1)},
{"a": 0, "b": 1, "law1": (1, 1), "law2": (0, 0)},
{"a": 1, "b": 0, "law1": (1, 1), "law2": (0, 0)},
{"a": 1, "b": 1, "law1": (0, 0), "law2": (0, 0)},
],
}
return {
"name": "demorgan_laws_exhaustive",
"summary": "对全部 4 组二元输入穷举验证德摩根第一、第二定律",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_combined_truth_table_and_laws():
pairs = [(0, 0), (0, 1), (1, 0), (1, 1)]
combined = [
[a, b, AND(a, b), OR(a, b), NOT(a), NOT(b), XOR(a, b)]
for a, b in pairs
]
# Boolean algebra laws: idempotent OR(1,1)=1, double negation NOT(NOT(a))=a
idempotent_ok = (OR(1, 1) == 1) and (AND(1, 1) == 1)
double_neg_ok = all(NOT(NOT(a)) == a for a in (0, 1))
actual = {
"combined_table": combined,
"idempotent_ok": idempotent_ok,
"double_neg_ok": double_neg_ok,
}
expected = {
"combined_table": [
[0, 0, 0, 0, 1, 1, 0],
[0, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 1, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 0],
],
"idempotent_ok": True,
"double_neg_ok": True,
}
return {
"name": "combined_truth_table_and_laws",
"summary": "校验交互真值表全量 7 列输出及布尔代数幂等律(1+1=1)与双重否定律",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_real_world_gate_scenarios():
actual = {
"vault_two_keys_both_present": AND(1, 1),
"vault_two_keys_one_missing": AND(1, 0),
"building_two_doors_one_open": OR(0, 1),
"building_two_doors_both_closed": OR(0, 0),
"status_sign_flip_from_free": NOT(0),
"two_lamps_mutex_valid": XOR(1, 0),
"two_lamps_mutex_alarm_when_same": NOT(XOR(1, 1)),
}
expected = {
"vault_two_keys_both_present": 1,
"vault_two_keys_one_missing": 0,
"building_two_doors_one_open": 1,
"building_two_doors_both_closed": 0,
"status_sign_flip_from_free": 1,
"two_lamps_mutex_valid": 1,
"two_lamps_mutex_alarm_when_same": 1,
}
return {
"name": "real_world_gate_scenarios",
"summary": "验证门禁双钥匙(AND)、双入口(OR)、状态翻转(NOT)与互斥双灯(XOR)四类现实逻辑场景",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_basic_gates_truth_tables(),
case_demorgan_laws_exhaustive(),
case_combined_truth_table_and_laws(),
case_real_world_gate_scenarios(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
两条定律分别为 `NOT(A AND B) = (NOT A) OR (NOT B)` 与 `NOT(A OR B) = (NOT A) AND (NOT B)`。它们允许工程师在 AND/OR/NOT 与更省晶体管的 NAND/NOR 门之间做等价变换,从而缩减门级数量与芯片延迟。