Code: The Hidden Language of Computer Hardware and Software 总结

Best Friends

在这个章节中,作者首先给出了这样一个现实场景:就是2个关系很好的小朋友,他们隔窗相望,想在晚上交流,但是由于父母的限制,他们没有电话,不能开灯,只有一个手电筒。在这种情况下,他们如何去交流想法呢?作者依次给出如下方法:

  1. 用手电筒在空中写字母
  2. 字母表中的每个字母都对应手电筒的闪烁,比如,A-1个闪烁; B-2个闪烁……
  3. 代替闪烁,而是用 Morse code 去表示相应的字母

这本书的名字就叫做 Code,那么什么是 Code 呢? 作者给出了如下的定义:

In this book, the word code usually means a system for transferring information among people and machines. In other words, a code lets you communicate.

根据这个定义,我们可以找出很多这样的系统。比如:英语单词表(当你和别人说 cat 的时候,对方就知道你指的是一只猫,而不是别的什么东西),Java 编程语言(当你写一句代码的时候,机器知道如何去执行它),手语(聋哑人可以用其交流)。我们其实还可以找到很多这样的系统。

其实我感觉作者眼中的 Code 是一种映射系统,那么为什么要有这层系统存在呢?大家可能会感觉多了这一层系统多麻烦啊!我想说的是,它们既然存在,就一定是在某种情况下,它们会更有优势。

比如,Morse code 为什么会被发明出来?大家可以想像一下,如果大家都是面对面的这种交流,英语单词表这种 Code 就完全足够了,如果有人用 Morse code 去交流,你可能会感觉这些人有脑子有问题。但是如果像上面那种远距离传递信息的情况呢?你传递 dot 和 dash 容易,还是 传递 letter 比较容易呢?

在有些情况下,你必须要创造出另一种 Code 才能去解决问题。比如一个正常人和一个失去了听觉,视觉,和说话能力的人来交流,显然英语单词表这种 Code 就不行,因此你需要创造出另一种让这2类人都可以理解的 Code,只有这样,他们才可以用这种 Code 去传递信息。

Codes and Combinations

Morse code 是一种 binary code,因为它只有2个组成要素 - a dot and a dash. 上一章节中是字母到 Morse code 的映射,这对发送 Morse code 的人很方便,但是对接收 Morse code 的人很不方便,这章中作者做了一个从 Morse code 到字母的映射,从只有1个元素开始,然后2个,3个,……

Braille and Binary Codes

如何让一个让盲人可以阅读呢? Valentin Haüy 发明了一套可以通过触摸来阅读的系统,这个系统纸上的字母都是凸起的。但是这个系统非常难用,只有几本书用这个系统。

Valentin Haüy 陷入了一个范式,他认为 A 在纸上就应该像 A 的样子,B 就是 B 的样子,这就像第1章节中用手电筒直接在空气中写字母一样。大家想一想,如果要是你应该去怎么解决这样一个问题。在回答这个问题之前,我们的系统应该是什么样的?首先,这个系统的目的就是要向盲人去传递信息,并且要容易在纸上表达出来。没错,聪明的 Louis Braille 发明出了一种 Code 做到了这点。下图中就是 Braille code:

Code: The Hidden Language of Computer Hardware and Software 总结

具体细节请参考书籍,这里我就不过多要介绍了。

Anatomy of a Flashlight

在这章中,作者主要阐述电流是如何产生的,从而使灯泡亮起来。

The chemical reaction can’t proceed unless there’s some way that the extra electrons can be taken away from the negative terminal of the battery and delivered back to the positive terminal. So if the battery isn’t connected to anything, nothing much happens. (Actually the chemical reactions still take place, but very slowly.) The reactions take place only if an electrical circuit is present to take electrons away from the negative side and supply electrons to the positive side. The electrons travel around this circuit in a counterclockwise direction:

Code: The Hidden Language of Computer Hardware and Software 总结

大家通常都会认为是电池提供电量到电路中。但是,从上面的介绍中,我们也可以认为是:电路提供一种方式,让电池发生化学反应。电路从电池的负极拿走电子,然后把它们运送到电池的正极。直到电池内的所有化学物质耗尽,电池内的反应会一直发生。

我们可以有这样一个类比:电流类似于管子中的水流,电压类似于水压,阻力类似于水管的宽度–管子越细,阻力越大。

Seeing Around Corners

你有个好朋友住在隔壁,在这种情况下,你就不能用手电筒去发送 Morse code 了,现在让我们自己用电池,灯泡,开关,和导线制作一个我们自己的手电筒吧!

Code: The Hidden Language of Computer Hardware and Software 总结

上图中的 V 代表电压,它也可以表示真空。我们可以把 V 看作是电子真空,把土地看作是电子的海洋,电子真空从土地上拉来电子,通过电路,电子经过灯泡,从而点亮它。上图中那个比较奇怪的符号(4个短线)代表 ground,a ground is a physical connection with the earth.

通过导线,我们可以摆脱距离的限制。通过提高电压,增加导线长度,减小电阻,从而可以使我们与远距离的朋友们互相交流。但是,导线的不可能无限长,按照上面的模式,最多也就能几百米远。那么我们如何来实现更远距离的信息传递呢?

Telegraphs and Relays

电报背后的思想其实很简单:You do something at one end of a wire that causes something to happen at the other end of the wire. 下图就是一个电报系统, When the telegraph key was pressed, the electromagnet in the sounder pulled the movable bar down and it made a “click” noise. When the key was released, the bar sprang back to its normal position, making a “clack” noise. A fast “click-clack” was a dot; a slower “click…clack” was a dash.

Code: The Hidden Language of Computer Hardware and Software 总结

电报的发明标志着现代通讯的开始,第一次,人们交流可以超过眼睛能看到的和耳朵能听到的距离,而且比马飞奔的速度还要快。尽管一些电报系统能进行大约300多米的通信,但是,我们依然做不到更远距离的通信,于是人们就想出了一个解决方案,发明了继电器,系统就被改进成下图的样子:

Code: The Hidden Language of Computer Hardware and Software 总结

继电器是一个伟大的设备,它无疑也是个开关,但是它的开与关不用人手去控制,而是用电流。

Our Ten Digits

这章主要介绍了我们今天使用的 number system - Hindu-Arabic or Indo-Arabic 相比与其它 number system 的好处,以及它的一些特点:

  • The Hindu-Arabic number system is said to be positional
  • There’s no special symbol for ten
  • Zero

The lowly zero is without a doubt one of the most important inventions in the history of numbers and mathematics. What’s best about the positional system of notation isn’t how well it works, but how well it works for counting systems not based on ten.

Alternatives to Ten

之所以我们人类的 number system 是 base ten 的,是因为我们本身就有10个手指,如果我们人类的手指像下图一样,那我们生活中的数字系统就很有可能是8进制的,那么我们的数字世界就只有0,1,2,3,4,5,6,7.

Code: The Hidden Language of Computer Hardware and Software 总结

上图的最右面的手指可以看到有个10,这里的10并不代表具体的数量,为了不混淆,你可以把它读成 one zero,它只是代表上图中手指的个数。如果我们在用这个8进制的系统,当数量达到10(one zero)时,我们就可以进位,这就是基于 positional 的 数字系统的伟大之处,你甚至可以用5进制,3进制,随你怎么做。

既然这样,看来2进制系统是最适合计算机的,In previous chapters, we’ve been looking at switches and wires and lightbulbs and relays, and any of these objects can represent the binary digits 0 and 1: A wire can be a binary digit. If current is flowing through the wire, the binary digit is 1. If not, the binary digit is 0. A switch can be a binary digit. If the switch is on, or closed, the binary digit is 1. If the switch is off, or open, the binary digit is 0. A lightbulb can be a binary digit. If the lightbulb is lit, the binary digit is 1. If the lightbulb is not lit, the binary digit is 0. A telegraph relay can be a binary digit. If the relay is closed, the binary digit is 1. If the relay is at rest, the binary digit is 0. Binary numbers have a whole lot to do with computers.

Bit by Bit by Bit

Letters and words and Morse code and Braille and decimal digits convey information as well. But in the computer age, the bit has come to be regarded as the basic building block of information.

Information 代表的是在2种或更多种可能之间的一种选择,比如,当我们用另一个人交流的时候,every word we speak is a choice among all the words in the dictionary. 如果给字典中的每个 word 用数字编号,我们也可以用数字来准确地传达我们想要传递的信息,当然了,沟通的2人之间要明白具体的数字表达什么含义。The flip side of this is that any information that can be reduced to a choice among two or more possibilities can be expressed using bits. 比如我们有2个bit,因此我们有4种可能,00,10,01,11,但是当我们想表达信息的时候,我们只会拿出这其中的一种可能去传递出去。同时,The more bits we have, the greater the number of different possibilities we can convey.

As we shall see later in this book, bits can represent words, pictures, sounds, music, and movies as well as product codes, film speeds, movie ratings, an invasion of the British army, and the intentions of one’s beloved. But most fundamentally, bits are numbers. All that needs to be done when bits represent other information is to count the number of possibilities. This determines the number of bits that are needed so that each possibility can be assigned a number.

Logic and Switches

与传统代数不一样的是,Boolean algebra 的操作数是类别,传统代数的那些法则它也一样可以用,比如:commutative, associative, and distributive rules,但是在 Boolean algebra 中, + 操作符 is distributive over the x 操作符:W+(BxF)=(W+B)x(W+F)

Gates (Not Bill)

用不同的方式连接 relays,我们可以组成不同的 logic gates,作者在这章中介绍了4个 logic gates 和 1个 inverter. 通过组合这些 logic gates,我们可以得到很复杂的电路,执行复杂的逻辑任务。4个 logic gates 的相应逻辑如下图所示:

Code: The Hidden Language of Computer Hardware and Software 总结

作者也介绍了 buffer,它的 output 与 它的 input 相同,它有如下2点作用:

  1. You can use a buffer when an input signal is weak
  2. A buffer can be used to slightly delay a signal. This works because the relay requires a little time—some fraction of a second—to be triggered

作者同时也提到了 De Morgan’s Laws,它的公式如下:

A¯¯¯×B¯¯¯=A+B¯¯¯¯¯¯¯¯¯A¯¯¯+B¯¯¯=A×B¯¯¯¯¯¯¯¯¯

从上面的公式我们可以看出,De Morgan’s Laws 是一个很重要的工具用来简化 Boolean 表达式,从而可以简化电路。

A Binary Adding Machine

在上面的章节中,我们已经学过了各种各样的 logic gates,用这些 logic gates 可以实现一个 Binary Adding Machine. 由于在做加法的过程中,会涉及到 sum 和 carry,因此作者在这个章节中引入了 XOR gate,下图是相应的对应关系:

Code: The Hidden Language of Computer Hardware and Software 总结

由于有了相应的 logic gates,我们只要把相应的表示 logic gates 的 relays 连接在一起,我们就可以做加法了。

But What About Subtraction?

上个章节中我们已经知道如何去做一个加法机器,加法需要进位,而减法需要借位,这是样的 logic 不容易在要计算机中表示,因此我们需要一些 trick 去做减法。假设我们现在有个10进制的计算机,当表示一个负数的时候,计算机不可能像我们人类一样去简单地在一个数字面前写上一个 - 代表负号,那么问题来了,我们如何在这个10进制计算机上表示负数呢,从而使得做减法就是加上一个负数,之后得到我们想要的结果

假设我们这个计算机最多可以表示3位数,即范围是0~999,如果你做加法超出了这个范围,就表示你的这个操作 overflow,最终导致的结果并不是你想要的,甚至有可能是很怪异的结果。如果你正常做加法,这个计算机完全没有问题,它可以得到你想要的结果,比如:100+500=600,做为一个有雄心的人来说,我并不满足于此,我想让它可以做减法,比如:888-666=?,或者 666-888=?

但是问题来了,我们如何用这个计算机来表示负数呢?由于我们这个计算机只能表示3位数,而我们又不能简单在这些数面前写个负号去表示负数,所以我们必须来调整上面表示的范围,现在变为-500~499,那么问题来了,我们怎么去表示负数呢?我们可以用下面的方式:

-500 —> 500
-499 —> 501
……
-1 —> 999
0 —> 000
……
497 —> 497
498 —> 498
499 —> 499

上面的这种方式叫做 ten’s complement, To convert a 3-digit negative number to ten’s complement, we subtract it from 999 and add 1. Well, using the ten’s complement, you don’t subtract numbers at all. Everything is addition. 比如对于上面的-499来说,用 ten’s complement 表示成 999-499+1=501

现在我们就可以用上面的计算机来做减法了:比如 888666,我们可以把它转换成 888+(666),而 666 用 ten’s complement 表示成 999666+1=334,因此 888+(666)=888+334=222; 在举个例子:如果是 666888,我们可以把它转换成 666+(888),而 888 用 ten’s complement 表示成 999888+1=112,因此 888+(666)=666+112=778222

同样的道理,我们完全可以把这套系统应用到2进制上,然后用 two’s complement 去表示负数。如果用 C 写过程序的同学会知道,它有 signed 和 unsigned 关键字,同样一串二进制,比如1000 0010,不同的关键字它会给你解读成不同的数,同时8 bit 所表示的范围也不相同。所以下图我们组成的电路既可以做加法,又可以做减法。

Code: The Hidden Language of Computer Hardware and Software 总结

当做减法的时候,SUB signal 要求 B input 转换成 Ones’ Complement,然后 +1,之后就是做加法了。