Boolean Logic
IGCSE Computer Science Topic 10 8:37 English narration · English + 中文 subtitles burned in
Chapters
Transcript
Look closely at this chip.
仔细看这块芯片。
There is nothing clever inside it. It cannot think.
它里面没有任何聪明的东西,它不会思考。
Inside are millions of tiny switches, and each switch can only be one of two things: on, or off.
里面是数百万个微小的开关,每个开关只能是两种状态之一:开,或者关。
We write on as one and off as zero.
我们把"开"写成一,把"关"写成零。
Everything a computer does is built on that.
计算机所做的一切,都建立在这之上。
So how does a machine decide anything?
那么机器是怎么做决定的?
A burglar alarm should ring only when the door is open and the system is switched on.
防盗报警器只有在门开着、 并且系统已经打开时才应该响。
That gives four possible situations, and it must get all four right.
这样一共有四种可能的情况,四种都必须判断正确。
Door shut, system off: silent.
门关、系统关:不响。
Door open, system off: silent, nobody armed it.
门开、系统关:还是不响,因为没人设防。
Door shut, system on: silent again.
门关、系统开:仍然不响。
Only in the last case does the alarm ring.
只有最后一种情况,报警器才响。
One rule, two inputs, one answer.
一条固定的规则,两个输入,一个答案。
That rule is a logic gate.
这条规则就是一个逻辑门。
Boolean logic.
布尔逻辑。
Today: the six gates you must know and exactly what each one does, how to read a logic circuit, how to turn a sentence into an expression, and how to fill a truth table without losing a row.
今天我们讲:你必须掌握的六个逻辑门,以及每一个到底做什么; 怎么看懂逻辑电路;怎么把一句话变成逻辑表达式; 还有怎么填真值表而不漏掉任何一行。
Then a full worked example, and the mistakes that cost marks.
然后是一道完整的例题,以及那些会失分的错误。
Start with the words.
先从术语开始。
Boolean logic uses only two values: true, written as one, and false, written as zero.
布尔逻辑只用两个值:真,写作一;假,写作零。
A logic gate is a small circuit that takes one or two inputs and gives exactly one output, by a fixed rule.
逻辑门是一个小电路,它接收一个或两个输入,按一条固定的规则给出恰好一个输出。
It never guesses.
它从不猜测。
A truth table then lists every possible set of inputs beside the output the gate gives.
而真值表,就是把每一种可能的输入组合,连同逻辑门给出的输出,全部列出来。
These are the six gates you must recognise and draw: NOT, AND, OR, NAND, NOR and XOR.
这就是你必须能认出并画出的六个逻辑门:非门、与门、或门、与非门、或非门和异或门。
NOT is a triangle, and it is the only gate with a single input.
非门是一个三角形,也是唯一只有一个输入的门。
The other five take two inputs, A and B.
另外五个都有两个输入,标为 A 和 B。
AND has a flat back and a round nose; OR has a curved back and a pointed nose.
与门后面是平的,前端是圆的。 或门后面是弯的,前端是尖的。
Now the detail that carries marks: the small circle on the output means invert.
现在是会给分的细节:输出端上的小圆圈表示取反。
NOT, NAND and NOR all have one.
非门、与非门和或非门都有一个。
Now watch three of them work.
现在看其中三个是怎么工作的。
We feed in A equals one and B equals zero.
我们输入 A 等于一、B 等于零。
The AND gate needs both inputs to be one, and B is zero, so its output is zero.
与门要求两个输入都是一,而 B 是零,所以输出是零。
The OR gate needs only one input to be one, and A is one, so its output is one.
或门只要有一个输入是一就行,而 A 是一,所以输出是一。
The NOT gate flips its single input, so its output is zero.
非门把它唯一的输入翻转,所以输出是零。
The AND gate in full.
完整的与门。
Two inputs give four rows, and you list them in this order every time so that none goes missing.
两个输入给出四行,而且你每次都按这个顺序列出,这样才不会漏掉哪一行。
Now read down the output column: zero, zero, zero, one.
现在从上往下看输出这一列:零、零、零、一。
AND gives an output of one only when both inputs are one.
与门只有在两个输入都是一时,输出才是一。
One zero anywhere, and the answer is zero.
只要有一个零,答案就是零。
Now the OR gate, and notice how little changes.
现在看或门,注意变化有多小。
The same four rows in the same order.
同样的四行,同样的顺序。
But the output column now reads zero, one, one, one.
但输出这一列现在是零、一、一、一。
OR gives one when at least one input is one.
或门只要至少有一个输入是一,输出就是一。
Only when both inputs are zero is the answer zero.
只有两个输入都是零,答案才是零。
Careful: in everyday English, or often means one or the other but not both.
要当心:在日常英语里,"或"常常表示二者取其一、 但不能同时。
In Boolean logic, one and one still gives one.
在布尔逻辑里,一和一,结果还是一。
That leaves the three gates students mix up, so build them from what you know.
剩下的三个是学生最容易混淆的,所以我们用已经学过的东西把它们推出来。
Here is the AND column again: zero, zero, zero, one.
这是刚才与门那一列:零、零、零、一。
Now flip every value and you have NAND: one, one, one, zero.
现在把每个值都翻转,就得到与非门:一、一、一、零。
NAND is AND followed by NOT. Do the same with OR.
与非门就是与门再接一个非门。
The OR column is zero, one, one, one. Flip that, and NOR is one, zero, zero, zero.
对或门做同样的事:或门那一列是零、一、一、一。
And XOR, the exclusive OR, gives one when the inputs differ, and zero when they are the same.
把它翻转,或非门就是一、零、零、零。 至于异或门,也就是"排他的或", 两个输入不同时输出一,相同时输出零。
One piece of logic can be written in four ways.
同一段逻辑可以用四种方式写出来。
A problem statement: the situation in ordinary words.
问题陈述:用普通语言描述的情境。
A logic expression: the same thing with letters and the words AND, OR and NOT.
逻辑表达式:同样的内容,用字母和 AND、OR、NOT 写出来。
A logic circuit: the gates drawn and joined by wires.
逻辑电路:把逻辑门画出来,用导线连起来。
And a truth table: every input combination with its output.
真值表:每一种输入组合以及它的输出。
The exam can give you any one of these four and ask for another.
考试可以给你其中任意一种,然后要你写出另一种。
A logic expression writes the circuit in words.
逻辑表达式就是用文字把电路写出来。
Take X equals, open bracket, A AND B, close bracket, OR, open bracket, NOT C, close bracket.
看这个:X 等于,左括号,A AND B,右括号, OR,左括号,NOT C,右括号。
Read it the way you read arithmetic: work out the brackets first, then combine.
像读算式一样读它:先算括号,再合起来。
With no brackets, NOT binds tightest, then AND, then OR.
如果没有括号,NOT 结合得最紧,然后是 AND,最后是 OR。
Do not lean on that. Write the brackets in.
但不要依赖这一点,把括号写出来。
Now the skill itself.
现在来练这项技能本身。
Back to our alarm: an alarm X sounds when the door is open, call that A, and the system is switched on, call that B.
回到我们的报警器。
Step one: pick out the conditions.
当门开着(记作 A), 并且系统已打开(记作 B)时,报警器 X 就响。
There are two, A and B.
第一步:找出条件,一共两个,A 和 B。
Step two: pick out the logic word.
第二步:找出逻辑词。
The sentence says and, so we need an AND gate.
句子里说的是"并且",所以我们需要一个与门。
Step three: write the expression.
第三步:写出表达式。
X is A AND B.
X 等于 A AND B。
Then draw it: one AND gate, inputs A and B, output X.
然后把它画出来:一个与门,输入 A 和 B,输出 X。
One warning: draw the circuit exactly as the sentence says.
一句提醒:句子怎么说,就怎么画。
Circuits get interesting when gates feed each other.
当逻辑门互相馈送时,电路才变得有意思。
Follow this one from the left.
从左边跟着这一条走。
A and B go into an AND gate.
A 和 B 进入一个与门。
That gate's output is not the answer: it becomes one input of the OR gate.
这个门的输出并不是最终答案,它成了或门的一个输入。
The other input of the OR gate is C.
或门的另一个输入是 C。
So X is, A AND B, then OR C.
所以 X 等于 A AND B,再 OR C。
At this level a circuit has at most three inputs and one output.
在这个阶段,一个电路最多三个输入、一个输出。
With three inputs the table grows.
有三个输入时,表就变大了。
Two choices for A, times two for B, times two for C: eight rows.
A 有两种选择,乘以 B 的两种,再乘以 C 的两种:八行。
Writing fewer rows has already cost you the mark.
写得比这少,分就已经丢了。
Getting them in order is easy if you count up in binary, from zero zero zero to one one one.
要按顺序写全其实很容易:按二进制往上数,从零零零一直数到一一一。
The last column flips every row, the middle column every two rows, the first column every four.
最后一列每行翻转一次,中间一列每两行一次,第一列每四行一次。
Here is our worked example drawn as a circuit.
这就是我们的例题,画成电路的样子。
X equals, A AND B, OR, NOT C.
X 等于 A AND B,再 OR NOT C。
A and B go into the AND gate at the top.
A 和 B 进入上面的与门。
C goes through the NOT gate below, which flips it.
C 通过下面的非门,被翻转。
The two results meet at the OR gate, and that produces X.
这两个结果在或门汇合,产生 X。
Now let us complete one row of its truth table.
现在我们来完成它真值表中的一行。
Take the row where A is one, B is zero and C is zero.
看 A 等于一、B 等于零、C 等于零这一行。
Work outwards from the brackets, one gate at a time.
从括号里往外做,一次算一个门。
First, A AND B.
先算 A AND B。
That is one AND zero, and AND needs both inputs to be one, so this part is zero.
那是一 AND 零,而与门要求两个输入都是一,所以这一部分是零。
Next, NOT C.
再算 NOT C。
C is zero, and NOT flips it, so this part is one.
C 是零,非门把它翻转,所以这一部分是一。
Finally, OR the two results: zero OR one.
最后把两个结果做 OR:零 OR 一。
OR needs only one input to be one, so X is one.
或门只要有一个输入是一,所以 X 等于一。
And the advice that earns marks: give each gate its own column.
还有一条能拿分的建议:给每个门单独开一列。
Even if the answer slips, those columns can still score.
就算最后的答案错了,这些中间列仍然可能得分。
Sometimes the exam gives you the finished table and asks for the expression.
有时候考试会给你填好的真值表,要你写出表达式。
Look down the output column and find every row where the output is one.
从上往下看输出这一列,找出每一个输出为一的行。
There are two here.
这里有两行。
Take the first of those rows.
先看第一行。
A is zero and B is one, so that part is NOT A AND B.
A 是零,B 是一,所以这一部分是 NOT A AND B。
An input of zero is written with NOT.
输入是零,就要写成 NOT。
Now the second row gives A AND NOT B.
再看第二行,得到 A AND NOT B。
Finally, join your parts with OR.
最后,把你写出的几部分用 OR 连起来。
And look at what you built: that is the XOR gate.
看看你做出了什么:这就是异或门。
Different inputs give one, the same give zero.
输入不同就是一,相同就是零。
One last thing, so you can see why this matters.
最后一件事,让你看到这些有什么用。
Join two gates and they can do arithmetic.
把两个逻辑门连起来,它们就能做算术。
This is a half adder: it adds two single bits.
这是一个半加器:它把两个单独的位相加。
The XOR gate gives the sum bit, one when the bits differ.
异或门给出"和"位,两位不同时为一。
The AND gate gives the carry bit, one only when both bits are one.
与门给出"进位"位,只有两位都是一时才为一。
So one plus one gives a sum of zero and a carry of one: two in binary.
所以一加一,得到和为零、进位为一,也就是二进制里的二。
Three marks students throw away.
三个学生常丢的分。
First, missing rows.
第一,漏行。
Three inputs means eight rows, always, in binary-counting order.
三个输入就是八行,永远是八行, 而且要按二进制计数的顺序。
Write the empty table out first.
先把空表画出来,再开始算。
Second, NAND and NOR.
第二,与非门和或非门。
They are the opposite of AND and OR, and the little circle on the output is your reminder.
它们和与门、或门正好相反,输出端那个小圆圈就是提醒。
Third, do not simplify.
第三,不要化简。
Draw the circuit exactly as it is written, even when you can see a shorter one.
题目怎么写,你就怎么画,哪怕你已经看出有更短的画法。