布尔逻辑 和 逻辑门

2020-05-04

Hi, I’m Carrie Anne and welcome to Crash Course Computer Science!

嗨,我是 Carrie Anne,欢迎收看计算机科学速成课!

Today we start our journey up the ladder of abstraction, where we leave behind the simplicity of being able to see every switch and gear, but gain the ability to assemble increasingly complex systems.

今天我们开始”抽象”的旅程。不用管底层细节,把精力用来构建更复杂的系统。

Last episode, we talked about how computers evolved from electromechanical devices, that often had decimal representations of numbers. like those represented by teeth on a gear to electronic computers with transistors that can turn the flow of electricity on or off.

上集,我们谈了计算机最早是机电设备,一般用十进制计数,比如用齿轮数来代表十进制,再到晶体管计算机。

And fortunately, even with just two states of electricity, we can represent important information. We call this representation Binary– which literally means “of two states”, in the same way a bicycle has two wheels or a biped has two legs.

幸运的是,只用 开/关 两种状态也可以代表信息。这叫 二进制,意思是”用两种状态表示”,就像自行车有两个轮,双足动物有两条腿。

You might think two states isn’t a lot to work with, and you’d be right! But, it’s exactly what you need for representing the values “true” and “false”.

你可能觉得两种状态不多,你是对的!但如果只需要表示 true 和 false,两个值就够了。

In computers, an “on” state, when electricity is flowing, represents true. The off state, no electricity flowing, represents false. We can also write binary as 1’s and 0’s instead of true’s and false’s. they are just different expressions of the same signal. we’ll talk more about that in the next episode.

电路闭合,电流流过,代表 “真”;电路断开,无电流流过,代表”假”。二进制也可以写成 1 和 0 而不是 true 和 false。只是不同的表达方式罢了!我们下集会讲更多细节。

Now it is actually possible to use transistors for more than just turning electrical current on and off, and to allow for different levels of current. Some early electronic computers were ternary, that’s three states, and even quinary, using 5 states.

晶体管的确可以不只是 开/关,还可以让不同大小的电流通过。一些早期电子计算机是三进制的,有 3 种状态,甚至五进制,5 种状态。

The problem is, the more intermediate states there are, the harder it is to keep them all seperate – if your smartphone battery starts running low or there’s electrical noise because someone’s running a microwave nearby,the signals can get mixed up…

问题是,状态越多,越难区分信号。如果手机快没电了或者附近有电噪音(因为有人在用微波炉),信号可能会混在一起…

And this problem only gets worse with transistors changing states millions of times per second! So, placing two signals as far apart as possible using just ‘on and off’ - gives us the most distinct signal to minimize these issues.

而每秒百万次变化的晶体管会让这个问题变得更糟!所以我们把两种信号尽可能分开,只用”开”和”关”两种状态,可以尽可能减少这类问题。

Another reason computers use binary is that an entire branch of mathematics already existed that dealt exclusively with true and false values.

计算机用二进制的另一个原因是:有一整个数学分支存在,专门处理”真”和”假”。

And it had figured out all of the necessary rules and operations for manipulating them. It’s called Boolean Algebra! George Boole, from which Boolean Algebra later got its name, was a self-taught English mathematician in the 1800s.

它已经解决了所有法则和运算,叫”布尔代数”!乔治·布尔(George Boole)是布尔二字的由来,是一位 19 世纪自学成才的英国数学家。

He was interested in representing logical statements that went “under, over, and beyond”, Aristotle’s approach to logic, which was, unsurprisingly, grounded in philosophy.

他有兴趣用数学式子 扩展亚里士多德基于哲学的逻辑方法。

Boole’s approach allowed truth to be systematically and formally proven, through logic equations which he introduced in his first book, “The Mathematical Analysis of Logic” in 1847.

布尔用 逻辑方程 系统而正式的证明真理(truth),他在 1847 年的第一本书”逻辑的数学分析”中介绍过。

In “regular” algebra – the type you probably learned in high school – the values of variables are numbers, and operations on those numbers are things like addition and multiplication.

在”常规”代数里 - 你在高中学的那种 - 变量的值是数字,可以进行加法或乘法之类的操作。

But in Boolean Algebra, the values of variables are true and false, and the operations are logical.

但在布尔代数中,变量的值是 true 和 false,能进行逻辑操作。

There are three fundamental operations in Boolean Algebra: a NOT, an AND, and an OR operation. And these operations turn out to be really useful so we’re going to look at them individually.

布尔代数中有三个基本操作:NOT, AND 和 OR。这些操作非常有用,我们一个个来看。

A NOT takes a single boolean value, either true or false, and negates it. It flips true to false, and false to true.

NOT 操作把布尔值反转,把 true 进行 NOT 就会变成 false,反之亦然

We can write out a little logic table that shows the original value under Input, and the outcome after applying the operation under Output.

我们可以根据 NOT 操作的输入和输出,做出这个表

Now here’s the cool part – we can easily build boolean logic out of transistors. As we discussed last episode, transistors are really just little electrically controlled switches.

酷的地方是 - 用晶体管可以轻松实现这个逻辑。上集说过,晶体管只是电控制的开关。

They have three wires: two electrodes and one control wire.When you apply electricity to the control wire, it lets current flow through from one electrode, through the transistor, to the other electrode. This is a lot like a spigot on a pipe – open the tap, water flows, close the tap, water shuts off.

有 3 根线:2 根电极和 1 根控制线。控制线通电时,电流就可以从一个电极流到另一个电极。就像水龙头一样,打开水龙头,就有水流出来;关掉水龙头,就没水了。

You can think of the control wire as an input, and the wire coming from the bottom electrode as the output. So with a single transistor, we have one input and one output.

可以把控制线,当做输入 ( input ) ; 底部的电极,当做输出(output)。所以 1 个晶体管,有一个输入和一个输出。

If we turn the input on, the output is also on because the current can flow through it. If we turn the input off, the output is also off and the current can no longer pass through.

如果我们打开输入(input on), 输出也会打开(output on) , 因为电流可以流过。如果关闭输入(input off) , 输出也会关闭(output off)。 因为电流无法通过!

Or in boolean terms, when the input is true, the output is true. And when the input is false, the output is also false. Which again we can show on a logic table.

或者用布尔术语来说:输入为 真,输出为 真。输入为 假 , 输出为 假。我们也可以把这个做成”真值表”!

This isn’t a very exciting circuit though because its not doing anything – the input and output are the same.But, we can modify this circuit just a little bit to create a NOT.

这个电路没什么意思,因为它没做什么事 – 输入和输出是一样的。但我们可以稍加修改,实现 NOT!

Instead of having the output wire at the end of the transistor, we can move it before.

与其把下面那根线当做 输出,我们可以把 输出 放到上面。

If we turn the input on, the transistor allows current to pass through it to the “ground”, and the output wire won’t receive that current, so it will be off.

如果打开 输入,电流可以流过然后 “接地”,输出就没有电流,所以输出是 off。输出就没有电流,所以输出是 off。

In our water metaphor grounding would be like if all the water in your house was flowing out of a huge hose, so there wasn’t any water pressure left for your shower. So in this case if the input is on, output is off.

如果用水来举例,就像家里的水都从一个大管子流走了,打开淋浴头一点水也没有。因此,如果输入是 on,输出是 off。

When we turn off the transistor, though, current is prevented from flowing down it to the ground, so instead, current flows through the output wire. So the input will be off and the output will be on.

当输入是 off,电流没法接地,就流过了输出,所以输出是 on。所以,如果输入是 off,输出是 on。

And this matches our logic table for NOT, so congrats, we just built a circuit that computes NOT! We call them NOT gates - we call them gates because they’re controlling the path of our current.

和 NOT 操作表一样!太棒了!我们做了个有点用的电路!我们叫它 “NOT 门” 。之所以叫 “门”,是因为它能控制电流的路径。

The AND Boolean operation takes two inputs, but still has a single output. In this case the output is only true if both inputs are true.

“AND”操作有 2 个输入,1 个输出。如果 2 个输入都是 true,输出才是 true。

Think about it like telling the truth. You’re only being completely honest if you don’t lie even a little. For example, let’s take the statement, “My name is Carrie Anne AND I’m wearing a blue dress”. Both of those facts are true, so the whole statement is true.

你可以想成是 说真话。如果完全不说谎,才是诚实。举例,看如下这个句子:我叫 Carrie Anne “而且”我穿着蓝色的衣服,2 个都是真的,所以整个是真的。

But if I said, “My name is Carrie Anne AND I’m wearing pants” that would be false, because I’m not wearing pants. Or trousers.If you’re in England.

但如果说,我叫 Carrie Anne”而且”我穿了裤子, 就是假的。因为我没穿裤子或长裤,如果你是英国人你会用这个词……(英/美单词不同梗)。

The Carrie Anne part is true, but a true AND a false, is still false. If I were to reverse that statement it would still obviously be false, and if I were to tell you two complete lies that is also false, and again we can write all of these combinations out in a table.

虽然前半句是真的,但是 真 “AND” 假,还是假。就算把前后顺序反过来,也依然是 假。如果我说 2 个假的事情,那么结果是假。和上次一样,可以给”AND”做个表。

To build an AND gate, we need two transistors connected together, so we have our two inputs and one output. If we turn on just transistor A, current won’t flow because the current is stopped by transistor B. Alternatively, if transistor B is on, but the transistor A is off, the same thing, the current can’t get through.

为了实现 “AND 门”,我们需要 2 个晶体管连在一起,这样有 2 个输入和 1 个输出。如果只打开 A,不打开 B 电流无法流到 output,所以输出是 false。如果只打开 B,不打开 A ,也一样,电流无法流到 output。

Only if transistor A AND transistor B are on does the output wire have current.

只有 A 和 B 都打开了,output 才有电流。

The last boolean operation is OR – where only one input has to be true for the output to be true.

最后一个是 OR (前面讲了 NOT 和 AND),只要 2 个输入里,其中 1 个是 true,输出就是 true。

For example, my name is Margaret Hamilton OR I’m wearing a blue dress. This is a true statement because although I’m not Margaret Hamilton unfortunately, I am wearing a blue dress, so the overall statement is true.

比如,我叫 Margaret Hamilton”或”我穿着蓝色衣服,结果是 true,虽然我不是 Margaret Hamilton,但是我穿着蓝色衣服,所以结果是 true。

An OR statement is also true if both facts are true. The only time an OR statement is false is if both inputs are false.

对于”OR 操作”来说,如果 2 个 输入都是 true,输出也是 true。只有 2 个输入都是 false,OR 的结果才是 false。

Building an OR gate from transistors needs a few extra wires. Instead of having two transistors in series – one after the other –we have them in parallel.

实现 “OR 门” 除了晶体管还要额外的线。不是串联起来,而是并联。

We run wires from the current source to both transistors. We use this little arc to note that the wires jump over one another and aren’t connected, even though they look like they cross.

然后左边这条线有电流输入,我们用”小拱门”代表 2 条线没连在一起,只是跨过而已,虽然看起来像连在一起。

If both transistors are turned off, the current is prevented from flowing to the output, so the output is also off.

如果 A 和 B 都是 off,电流无法流过,所以输出是 off。

Now, if we turn on just Transistor A, current can flow to the output. Same thing if transistor A is off, but Transistor B in on. Basically if A OR B is on, the output is also on. Also, if both transistors are on, the output is still on.

如果打开 A,电流可以流过。输出是 on。如果只打开 B 也一样,只要 A OR B 是 on, 输出就是 on。如果 A 和 B 都 on,结果是 on。

Ok, now that we’ve got NOT, AND, and OR gates, and we can leave behind the constituent transistors and move up a layer of abstraction.

好,现在 NOT 门, AND 门, OR 门 都搞定了,我们可以进行一次抽象。

The standard engineers use for these gates are a triangle with a dot for a NOT, a D for the AND, and a spaceship for the OR. Those aren’t the official names, but that’s howI like to think of them.

NOT 门的画法是三角形前面一个圆点,AND 门用 D 表示 ,OR 门用太空船表示。”D 形状和太空船”不是标准叫法, 只是我喜欢这样叫而已!

Representing them and thinking about them this way allows us to build even bigger components while keeping the overall complexity relatively the same. just remember that that mess of transistors and wires is still there.

我们可以用这种方法表示它们,构建更大的组件,就不会变得很复杂。晶体管和电线依然在那里,我们只是用符号来代表而已。

For example, another useful boolean operation in computation is called an Exclusive OR, or XOR for short.

除了前面说的三个 另一个有用的布尔操作叫 “异或”,简称 XOR。

XOR is like a regular OR, but with one difference: if both inputs are true, the XOR is false. The only time an XOR is true is when one input is true and the other input is false.

XOR 就像普通 OR,但有一个区别:如果 2 个输入都是 true,XOR 输出 false。想要 XOR 输出 true \N 一个输入必须是 true,另一个必须是 false。

It’s like when you go out to dinner and your meal comes with a side salad OR a soup, sadly, you can’t have both!

就像你出去吃晚饭,你点的饭要么配沙拉,要么配汤,你不能两个都要!

And building this from transistors is pretty confusing, but we can show how an XOR is created from our three basic boolean gates.

用晶体管实现 XOR 门有点烧脑子,但我可以展示一下 怎么用前面提到的 3 种门来做 XOR 门。

We know we have two inputs again – A and B – and one output. Let’s start with an OR gate, since the logic table looks almost identical to an OR.

我们有 2 个输入,A 和 B ,还有 1 个输出. 我们先放一个 OR 门. 因为 OR 和 XOR 的逻辑表很像。

There’s only one problem - when A and B are true, the logic is different from OR, and we need to output “false”.

只有 1 个问题 - 当 A 和 B 都是 true 时 \N OR 的输出和想要的 XOR 输出不一样,我们想要 false。

And XOR turns out to be a very useful component, and we’ll get to it in another episode, so useful in fact engineers gave it its own symbol too – an OR gate with a smile :)

XOR 超有用的,我们下次再说它。因为超有用,工程师给了它一个符号,一个 OR 门 + 一个笑脸。

But most importantly, we can now put XOR into our metaphorical toolbox and not have to worry about the individual logic gates that make it up, or the transistors that make up those gates, or how electrons are flowing through a semiconductor.

重要的是,现在可以把 XOR 放入”工具箱”了,不用担心 XOR 具体用了几个门,这几个门又是怎么用晶体管拼的,或电子是怎么流过半导体的。

Moving up another layer of abstraction. When computer engineers are designing processors, they rarely work at the transistor level, and instead work with much larger blocks, like logic gates, and even larger components, made up of logic gates, which we’ll discuss in future episodes.

再次向上抽象:工程师设计处理器时,很少在晶体管的层面上思考,而是用更大的组件,比如逻辑门,或者由逻辑门组成的更大组件,我们以后会讲。

And even if you are a professional computer programmer, it’s not often that you think about how the logic that you are programming is actually implemented in the physical world by these teeny tiny components.

就算是专业程序员,也不用考虑逻辑是怎样在物理层面实现的。

We’ve also moved from thinking about raw electrical signals to our first representation of data - true and false - and we’ve even gotten a little taste of computation.

我们从电信号开始,到现在第一次表示数据 - 真和假 - 开始有点”计算”的感觉了。

With just the logic gates in this episode, we could build a machine that evaluates complex logic statements, like if “Name is John Green AND after 5pm OR is Weekend AND near Pizza Hut”, then “John will want pizza” equals true.

我们可以判断复杂的语句,仅用这集讲的 逻辑门,比如:[如果是 John Green] AND [下午 5 点后] OR [周末] AND [在比萨店附近],那么 “John 想要比萨” = 真。