3 Boolean Logic & Logic Gates

3 布尔逻辑和逻辑门

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

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

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

二进制也可以写成1和0而不是true和false

they are just different expressions of the same signal

只是不同的表达方式罢了

but we'll talk more about that in the next episode.

我们下集会讲更多细节

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,

一些早期电子计算机是三进制的,有3种状态

and even quinary, using 5 states.

甚至五进制,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,

乔治·布尔(GeorgeBoole)是布尔二字的由来

was a self-taught English mathematician in the 1800s.

是一位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

布尔用逻辑方程系统而正式的证明真理(truth)

which he introduced in his first book, "The Mathematical Analysis of Logic" in 1847.

他在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.

布尔代数中有三个基本操作:NOT,AND和OR

And these operations turn out to be really useful so we're going to look at them individually.

这些操作非常有用,我们一个个来看

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

NOT操作把布尔值反转,

It flips true to false, and false to true.

把true进行NOT就会变成false,反之亦然

We can write out a little logic table that shows the original value under Input,

我们可以根据NOT操作的输入和输出,

and the outcome after applying the operation under Output.

做出这个表

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.

有3根线:2根电极和1根控制线

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.

关掉水龙头,就没水了

You can think of the control wire as an input,

and the wire coming from the bottom electrode as the output.

可以把控制线,当做输入(input)底部的电极,当做输出(output)

So with a single transistor, we have one input and one output.

所以1个晶体管,有一个输入和一个输出

If we turn the input on, the output is also on because the current can flow through it.

如果我们打开输入(input on)输出也会打开(output on) 因为电流可以流过

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

如果关闭输入(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

这个电路没什么意思,因为它没做什么事

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

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.

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

So the input will be off and the output will be on.

如果输入是off,输出是on

And this matches our logic table for NOT, so congrats, we just built a circuit that computes NOT!

和NOT操作表一样!太棒了!我们做了个有点用的电路!

We call them NOT gates we call them gates because they're controlling the path of our current.

我们叫它"NOT门" 之所以叫"门",是因为它能控制电流的路径

The AND Boolean operation takes two inputs, but still has a single output.

AND操作有2个输入,1个输出

In this case the output is only true if both inputs are true.

如果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.

我叫CarrieAnne"而且"我穿着蓝色的衣服

Both of those facts are true, so the whole statement is true.

2个都是真的,所以整个是真的

But if I said, "My name is Carrie Anne AND I'm wearing pants" that would be false,

但如果说,我叫CarrieAnne"而且"我穿了裤子,就是假的

because I'm not wearing pants.

因为我没穿裤子

Or trousers.

或长裤,如果你是英国人你会用这个词……(英/美单词不同梗)

If you're in England.

或长裤,如果你是英国人你会用这个词……(英/美单词不同梗)

The Carrie Anne part is true, but a true AND a false, is still false.

虽然前半句是真的,但是真"AND"假,还是假

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,

如果我说2个假的事情,那么结果是假。

and again we can write all of these combinations out in a table.

和上次一样,可以给"AND"做个表

To build an AND gate, we need two transistors connected together

为了实现"AND门",我们需要2个晶体管连在一起

so we have our two inputs and one output.

这样有2个输入和1个输出

If we turn on just transistor A, current won't flow because the current is stopped by transistor B.

如果只打开A,不打开B 电流无法流到output,所以输出是false

Alternatively, if transistor B is on, but the transistor A is off,

the same thing, the current can't get through.

如果只打开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

最后一个是OR(前面讲了NOT和AND)

-where only one input has to be true for the output to be true.

只要2个输入里,其中1个是true,输出就是true

For example, my name is Margaret Hamilton OR I'm wearing a blue dress.

比如,我叫MargaretHamilton"或"我穿着蓝色衣服

This is a true statement because although I'm not Margaret Hamilton unfortunately,

结果是true,虽然我不是MargaretHamilton

I am wearing a blue dress, so the overall statement is true.

但是我穿着蓝色衣服,所以结果是true

An OR statement is also true if both facts are true.

对于"OR操作"来说, 如果2个输入都是true,输出也是true

The only time an OR statement is false is if both inputs are false.

只有2个输入都是false,OR的结果才是false

Building an OR gate from transistors needs a few extra wires.

实现"OR门"除了晶体管还要额外的线

Instead of having two transistors in series -one after the other --

we have them in parallel.

不是串联起来。而是并联

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,

我们用"小拱门"代表2条线没连在一起,只是跨过而已

even though they look like they cross.

虽然看起来像连在一起

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

如果A和B都是off,电流无法流过

so the output is also off.

所以输出是off

Now, if we turn on just Transistor A, current can flow to the output.

如果打开A,电流可以流过。输出是on

Same thing if transistor A is off, but Transistor B in on.

如果只打开B也一样

Basically if A OR B is on, the output is also on.

只要AORB是on,输出就是on

Also, if both transistors are on, the output is still on.

如果A和B都on,结果是on

Ok, now that we've got NOT, AND, and OR gates,

好,现在NOT门,AND门,OR门都搞定了

and we can leave behind the constituent transistors and move up a layer of abstraction.

我们可以进行一次抽象

The standard engineers use for these gates are a triangle with a dot for a NOT,

NOT门的画法是三角形前面一个圆点

a D for the AND, and a spaceship for the OR.

AND门用D表示,OR门用太空船表示

Those aren't the official names, but that's howI like to think of them.

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:

XOR就像普通OR,但有一个区别:

if both inputs are true, the XOR is false.

如果2个输入都是true,XOR输出false

The only time an XOR is true is when one input is true and the other input is false.

想要XOR输出true 一个输入必须是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,

用晶体管实现XOR门有点烧脑子

but we can show how an XOR is created from our three basic boolean gates.

但我可以展示一下 怎么用前面提到的3种门来做XOR门

We know we have two inputs again -A and B -and one output.

我们有2个输入,A和B,还有1个输出.

Let's start with an OR gate, since the logic table looks almost identical to an OR.

我们先放一个OR门.因为OR和XOR的逻辑表很像

There's only one problem when A and B are true, the logic is different from OR,

只有1个问题当A和B都是true时 OR的输出和想要的XOR输出不一样

and we need to output "false".

我们想要false

And XOR turns out to be a very useful component,

XOR超有用的

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 :)

因为超有用, 工程师给了它一个符号,一个OR门+一个笑脸

But most importantly, we can now put XOR into our metaphorical toolbox

重要的是,现在可以把XOR放入"工具箱"了

and not have to worry about the individual logic gates that make it up,

不用担心XOR具体用了几个门

or the transistors that make up those gates,

这几个门又是怎么用晶体管拼的

or how electrons are flowing through a semiconductor.

或电子是怎么流过半导体的

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",

[如果是JohnGreen]AND[下午5点后]OR[周末]AND[在比萨店附近]

then "John will want pizza" equals true.

那么"John想要比萨"=真

And with that, I'm starving, I'll see you next week.

我都说饿了,下周见