Why Computers Use Binary
IGCSE Computer Science Topic 1 21:16 English narration · English + 中文 subtitles burned in
Chapters
Transcript
Inside a processor there are billions of tiny switches.
处理器里有数十亿个微小的开关。
Each one can only be on, or off.
每一个都只能是开,或者关。
That is all a computer physically has — no threes, no sevens, no letters.
这就是计算机在物理上拥有的全部——没有三,没有七,也没有字母。
So every number you type, every photo, every song and every message has to be turned into a pattern of ons and offs before the machine can touch it.
所以你敲下的每一个数字、每一张照片、每一首歌、每一条消息, 都必须先变成一串"开"和"关",机器才碰得到它。
We write on as one and off as zero, and we call that binary.
我们把"开"写成一,"关"写成零, 这就叫二进制。
This whole lesson is really one question: how do you build everything out of just two symbols?
整节课其实只有一个问题:只用两个符号,怎么把一切都搭出来?
A microprocessor is made of millions of tiny transistors.
微处理器由数百万个微小的晶体管组成。
Each transistor is a switch that is on or off — that is the physical basis of binary.
每个晶体管都是一个开关,只能开或者关—— 这就是二进制的物理基础。
The processor uses logic gates to combine those ons and offs, and it holds the results in registers: small, very fast stores inside the chip.
处理器用逻辑门把这些开和关组合起来, 并把结果放进寄存器:芯片内部又小又快的存储单元。
Every later idea in this lesson — numbers, text, sound, images — is just patterns of ones and zeros moving through those gates and sitting in those registers.
这节课后面的每一个概念——数字、文本、声音、图像—— 都只是在这些逻辑门里流动、在这些寄存器里暂存的一串一和零。
Binary, and everything built on top of it.
二进制,以及建立在它之上的一切。
Today: number systems and place value, converting between denary, binary and hex, binary addition and overflow, logical shifts, two's complement, how text, sound and images are stored, file size, and compression.
今天我们讲:数制与位值、十进制与二进制和十六进制之间的转换、 二进制加法与溢出、逻辑移位、补码、文本与声音和图像是怎么存储的、文件大小,以及压缩。
Let's begin.
让我们开始吧。
First, three number systems.
首先是三种数制。
Denary is the everyday one — base ten, with the digits zero to nine.
十进制是我们日常用的那种——以十为基数,数字从零到九。
Binary is base two: only zero and one.
二进制以二为基数:只有零和一。
Hexadecimal is base sixteen: zero to nine, then A to F standing for ten up to fifteen.
十六进制以十六为基数:零到九,然后用 A 到 F 表示十到十五。
The base simply tells you how many different digits the system uses.
基数不过是告诉你,这个系统一共用几种不同的数字。
In binary each column has a place value, and they double from right to left: one, two, four, eight, sixteen, thirty-two, sixty-four, one hundred and twenty-eight.
在二进制里,每一列都有一个位值,从右往左依次翻倍:一、二、四、八、十六、三十二、 六十四、一百二十八。
Three words to know as well: one bit is a single zero or one, four bits make a nibble, and eight bits make a byte.
还有三个词要记住:一个位就是单独的一个零或一,四个位是半字节, 八个位是一个字节。
Look at an eight-bit place-value chart.
看这张八位的位值表。
From left to right the columns are one hundred and twenty-eight, sixty-four, thirty-two, sixteen, eight, four, two and one.
从左到右,各列分别是一百二十八、六十四、三十二、十六、八、四、二、一。
Under those columns sit the bits of one hundred and fifty: one, zero, zero, one, zero, one, one, zero.
这些列下面放着一百五十的各位:一、零、零、一、零、一、一、零。
Add the place values that hold a one: one hundred and twenty-eight plus sixteen plus four plus two equals one hundred and fifty.
把下面是一的那些位值加起来:一百二十八加十六加四加二,正好等于一百五十。
That is what place value does — each column is worth twice the one on its right.
这就是位值的作用——每一列的值,都是右边那一列的两倍。
Counting in binary makes the place-value idea concrete.
用二进制数数,能把位值的概念变得具体。
Start at zero: all bits off.
从零开始:所有位都是关。
One is just a one in the ones column.
一就是在个位列写一个一。
Two is a one in the twos column.
二就是在二的那一列写一个一。
Three is both of those together.
三则是这两列都写一。
Keep going and you will hit fifteen — four bits all set to one.
继续数下去你会到十五—— 四个位全部是一。
That is why four bits can hold zero through fifteen, which is exactly one hex digit's worth of values.
所以四个位正好能表示从零到十五,也正好是一个十六进制数字能表示的全部值。
Cambridge questions can stretch binary numbers out to sixteen bits, but the rule never changes: each new column doubles the place value.
剑桥考题里的二进制数可以长到十六位,但规则从不改变:每往左多一列,位值就翻一倍。
Now converting.
现在来做转换。
Denary to binary: write out the place values, then work from the left, taking each value that still fits.
十进制转二进制:先把位值写出来,然后从左往右,把还装得下的值一个个取走。
Change one hundred and fifty.
我们来转一百五十。
One hundred and twenty-eight fits, so put a one there — that leaves twenty-two.
一百二十八装得下,就在那里写一——还剩二十二。
Sixty-four is too big, so zero.
六十四太大了,写零。
Thirty-two, too big, zero.
三十二,太大,写零。
Sixteen fits, so one — six left.
十六装得下,写一——还剩六。
Eight is too big, zero.
八太大,写零。
Four fits, one — two left.
四装得下,写一——还剩二。
Two fits, one — nothing left.
二装得下,写一——什么都不剩了。
And one, zero.
最后的一,写零。
Going back the other way is easier still: just add up the place values that have a one underneath them.
反过来转就更简单了: 只要把下面写着一的那些位值加起来就行。
Going from binary back to denary is the reverse of what we just did.
从二进制转回十进制,就是刚才过程的逆运算。
Take one zero zero one zero one one zero again.
再看一遍一零零一零一一零。
Under each one, pick up its place value: one hundred and twenty-eight, sixteen, four and two.
在每一个一的下面,取出它的位值:一百二十八、十六、四和二。
Add them.
把它们加起来。
One hundred and twenty-eight plus sixteen is one hundred and forty-four; plus four is one hundred and forty-eight; plus two is one hundred and fifty.
一百二十八加十六是一百四十四;再加四是一百四十八;再加二是一百五十。
Whenever an exam says "convert this binary to denary," that is the whole method — add the place values that hold a one, and ignore the zeros.
考试里只要说"把这个二进制转成十进制",方法就是这一个—— 把下面是一的位值加起来,零的那些列直接忽略。
Hexadecimal looks strange, but it exists for one simple reason: it is short.
十六进制看上去有点怪,但它存在的理由很简单:它短。
One hex digit stands for exactly four binary digits — one nibble.
一个十六进制数字, 正好代表四个二进制数字——也就是一个半字节。
So to go from hex to binary, expand each hex digit into its own four bits.
所以从十六进制转二进制, 就把每个十六进制数字展开成它自己的四个位。
F becomes one-one-one-one, zero becomes zero-zero-zero-zero, and eight becomes one-zero-zero-zero.
F 变成一一一一,零变成零零零零, 八变成一零零零。
Going the other way, group the bits into fours starting from the right, and turn each group into a single hex digit.
反过来,从右边开始把二进制每四个分成一组,再把每一组变成一个十六进制数字。
The value never changes — hex is just a shorter way of writing the very same binary.
数值从头到尾没有变——十六进制只是同一串二进制的一种更短的写法。
That is why you meet it in colour codes, MAC addresses, memory addresses and error codes.
所以你会在颜色代码、MAC 地址、内存地址和错误码里见到它。
Here is why hex is short.
这就是十六进制为什么短。
Look at F zero eight.
看 F 零八。
Each hex digit expands into its own four-bit nibble.
每个十六进制数字都展开成自己的四位半字节。
F is fifteen in denary, which is one-one-one-one in binary.
F 在十进制里是十五,二进制是一一一一。
Zero is zero-zero-zero-zero.
零是零零零零。
Eight is one-zero-zero-zero.
八是一零零零。
Stick the three nibbles together and you have twelve bits: one-one-one-one zero-zero-zero-zero one-zero-zero-zero.
把三个半字节拼在一起,就是十二个位:一一一一、零零零零、一零零零。
Going the other way, start from the right, group every four bits, and turn each group into one hex digit.
反过来,从右边开始,每四个位分成一组,再把每一组变成一个十六进制数字。
You never change the value — you only change how short the writing is.
数值从不改变——你改变的只是写法有多短。
The easy path from denary to hex is binary in the middle.
从十进制到十六进制,最轻松的路径是中间先过二进制。
Convert denary one hundred to eight-bit binary first.
先把十进制一百转成八位二进制。
One hundred is sixty-four plus thirty-two plus four, so the bits are zero one one zero zero one zero zero.
一百等于六十四加三十二加四,所以各位是零一一零零一零零。
Now split into nibbles from the right: zero-one-one-zero and zero-one-zero-zero.
再从右边分成半字节:零一一零和零一零零。
Zero-one-one-zero is six; zero-one-zero-zero is four.
零一一零是六;零一零零是四。
So denary one hundred is hex six-four.
所以十进制一百就是十六进制的六四。
Memorise the letter table too: ten is A, eleven is B, twelve is C, thirteen is D, fourteen is E, and fifteen is F — each with its own four-bit pattern from one-zero-one-zero up to one-one-one-one.
字母表也要背下来:十是 A,十一是 B,十二是 C, 十三是 D,十四是 E,十五是 F——各自对应从一零一零到一一一一的四位模式。
Where do you actually meet hex?
你实际会在哪里碰到十六进制?
Colour codes in web pages: hash F F zero zero zero zero is pure red, because each pair of hex digits packs one byte for red, green or blue.
网页里的颜色代码:井号后面 F F 零零零零就是纯红, 因为每两个十六进制数字打包一个字节,分别表示红、绿或蓝。
Network hardware uses hex in MAC addresses, and IPv6 addresses are written in hex too.
网络硬件在 MAC 地址里用十六进制, 而且 IPv6 地址也是用十六进制写的。
Memory addresses and error codes are almost always hex, because a short string of hex digits stands for a long string of bits without mistakes.
内存地址和错误码几乎总是十六进制, 因为一串短的十六进制数字就能代表一长串二进制位,还不容易写错。
A memory dump — a print-out of what is sitting in memory — is shown in hex for the same reason: humans can read it, and the value is still the original binary.
内存转储——也就是把内存里内容打印出来——用十六进制显示,理由相同: 人读得懂,而数值仍然是原来的二进制。
Binary addition works column by column from the right, exactly like denary.
二进制加法是从右往左一列一列地做的,和十进制完全一样。
There are only four cases to learn.
只有四种情况要记。
Zero plus zero is zero.
零加零得零。
Zero plus one is one.
零加一得一。
One plus zero is one.
一加零得一。
And one plus one is zero, carry one.
而一加一得零,进位一。
If a carry comes in as well, then one plus one plus one is one, carry one.
如果还有一个进位进来, 那么一加一加一得一,进位一。
Here is one hundred and eighteen plus forty-eight.
这里是一百一十八加四十八。
Work from the right, letting each carry ripple along to the left, and the answer comes out as one hundred and sixty-six.
从右边开始做, 让每一个进位一路向左传递,答案就是一百六十六。
Walk the addition again with the carries visible.
再把这次加法走一遍,并看清进位。
Top row is zero one one one zero one one zero — one hundred and eighteen.
上面一行是零一一一零一一零——一百一十八。
Bottom row is zero zero one one zero zero zero zero — forty-eight.
下面一行是零零一一零零零零——四十八。
From the right: zero plus zero is zero; one plus zero is one; one plus zero is one; then zero plus zero is zero.
从右边起:零加零得零;一加零得一;一加零得一; 然后零加零得零。
Next columns start producing carries when ones meet ones.
后面几列里,一碰上一时就开始产生进位。
The carries ripple left in red on the diagram, and the final eight bits are one zero one zero zero one one zero — one hundred and sixty-six.
图上红色的进位一路向左传递,最后八位是一零一零零一一零——一百六十六。
Only four column rules, plus the carry-in case of one plus one plus one.
记住的只有四种列规则,外加进位进来时一加一加一那一种情况。
But a register has a fixed size.
但寄存器的大小是固定的。
An eight-bit register can only hold values from zero up to two hundred and fifty-five.
一个八位寄存器只能存放从零到二百五十五的值。
So watch what happens when we add two hundred, and seventy-two.
所以来看看,当我们把二百和七十二加起来时会发生什么。
The true answer is two hundred and seventy-two — and in binary that needs nine bits.
真正的答案是二百七十二—— 而它在二进制里需要九个位。
The register only has eight.
寄存器却只有八位。
The ninth bit, the leading one, has nowhere to go, so it is thrown away, and the stored answer is wrong.
第九位,也就是最前面的那个一,无处安放, 于是被丢掉了,存下来的答案就是错的。
That is overflow: the result needs more bits than the register has room for.
这就是溢出:结果需要的位数,超过了寄存器能容纳的位数。
See the overflow trap clearly.
把溢出的陷阱看清楚。
Two hundred is one one zero zero one zero zero zero.
二百是一一零零一零零零。
Seventy-two is zero one zero zero one zero zero zero.
七十二是零一零零一零零零。
Their true sum is two hundred and seventy-two, which in binary needs a leading one — nine bits in total.
它们真正的和是二百七十二,二进制里需要最前面多一个一——一共九位。
An eight-bit register keeps only the lower eight bits, which read as sixteen in denary, not two hundred and seventy-two.
八位寄存器只留下后面八位,读出来是十进制的十六,而不是二百七十二。
The leading one was thrown away.
最前面的那个一被丢掉了。
That is an overflow error: the result went outside the limit the register can store, and the stored answer is simply wrong.
这就是溢出错误:结果超出了寄存器能存放的范围, 存下来的答案就是错的。
A logical binary shift slides every bit left or right by a number of places.
逻辑二进制移位,把每一个位整体向左或向右滑动若干位。
Zeros come in at the empty end, and any bit pushed off the other end is simply lost — gone for good.
空出来的那一端补上零, 而被挤出另一端的位就直接丢失了——再也找不回来。
A left shift of one place doubles the number; two places multiplies it by four.
左移一位,数值翻一倍; 左移两位,就乘以四。
A right shift halves it each time.
右移则每次除以二。
Take fifty-three and shift it left by two.
我们把五十三左移两位。
Every bit moves two columns to the left, two zeros come in on the right, and the two bits on the far left drop off the end.
每一个位都往左移动两列,右边补进两个零,最左边的两个位掉出了末端。
The answer is two hundred and twelve, which is fifty-three times four.
答案是二百一十二,正好是五十三乘以四。
On the shift diagram, every bit of fifty-three slides two places left.
在移位图上,五十三的每一位都向左滑两位。
The two left-most bits fall off the end and are lost for good — if either was a one, that information is gone.
最左边的两位掉出末端,永久丢失—— 如果其中有一,那部分信息就没了。
Two zeros fill the empty places on the right.
右边空出来的位置补上两个零。
The result, two hundred and twelve, is fifty-three times four, because each left place multiplies by two.
结果二百一十二等于五十三乘以四,因为每左移一位就乘以二。
A right shift works the opposite way: bits slide right, zeros enter on the left, and the right-most bits — the least significant bits — drop off.
右移正好相反:位向右滑,左边补零,最右边的位——最低有效位——掉出去。
Each right place halves the number, but only when no ones are lost at the edge.
每右移一位就除以二,但前提是边缘没有一被丢掉。
So far everything has been positive.
到目前为止,所有数都是正的。
Two's complement lets the same eight bits hold negative numbers too.
补码让同样这八个位也能表示负数。
The trick is that the left-most bit — the most significant bit — is given a negative place value: minus one hundred and twenty-eight.
诀窍在于:最左边那一位——也就是最高有效位——被赋予了一个负的位值:负一百二十八。
If that bit is zero the number is positive; if it is one, the number is negative.
如果这一位是零,这个数就是正的;如果是一,它就是负的。
To make a number negative, write the positive version, flip every bit, then add one.
要把一个数变成负数, 先写出它的正数形式,把每一位取反,然后加一。
For minus forty: positive forty first, then flip all eight bits, then add one.
以负四十为例:先写正四十, 再把八个位全部取反,然后加一。
Check it by adding the place values — minus one hundred and twenty-eight, plus sixty-four, plus sixteen, plus eight — and you get minus forty.
用位值加起来验算一下——负一百二十八,加六十四,加十六, 加八——正好得到负四十。
The range of an eight-bit two's complement number is minus one hundred and twenty-eight up to plus one hundred and twenty-seven.
八位补码能表示的范围,是从负一百二十八到正一百二十七。
Build minus forty step by step on the chart.
在图上一步一步做出负四十。
Positive forty is zero zero one zero one zero zero zero.
正四十是零零一零一零零零。
Flip every bit: one one zero one zero one one one.
把每一位取反:一一零一零一一一。
Add one: the last bits become one zero zero zero, giving one one zero one one zero zero zero.
再加一:末尾几位变成一零零零,得到一一零一一零零零。
Now read it with the most significant bit worth minus one hundred and twenty-eight: minus one hundred and twenty-eight, plus sixty-four, plus sixteen, plus eight equals minus forty.
现在用最高有效位等于负一百二十八来读:负一百二十八,加六十四,加十六,加八,等于负四十。
Positive numbers keep the most significant bit at zero; negative numbers set it to one.
正数把最高有效位保持为零;负数则把它设为一。
The full eight-bit range is minus one hundred and twenty-eight up to plus one hundred and twenty-seven — not up to two hundred and fifty-five, because half the patterns are now negatives.
八位的完整范围是负一百二十八到正一百二十七——到不了二百五十五, 因为有一半的模式现在表示负数。
Now real data.
现在来看真实的数据。
Text is stored by giving every character a number, and then storing that number in binary.
文本的存储方式,是给每一个字符分配一个数字,再把这个数字以二进制存下来。
The list of characters together with their numbers is called a character set.
字符及其对应数字的这份清单,就叫字符集。
ASCII uses seven bits per character, which gives one hundred and twenty-eight different characters — enough for English letters, digits and the common symbols.
ASCII 每个字符用七个位, 一共能表示一百二十八个不同的字符——足够放下英文字母、数字和常用符号。
Unicode uses more bits per character, so it can cover many languages, plus symbols and emoji.
Unicode 每个字符用更多的位,所以它能覆盖很多种语言,还有各种符号和表情符号。
The trade is simple: more characters means more bits for each one, so the same text takes more storage.
这个取舍很简单:字符越多,每个字符要用的位就越多,同样的文本占的存储也就越大。
A character set is just a table: this character maps to this number, and that number is stored in binary.
字符集其实就是一张表:这个字符对应这个数字,那个数字再以二进制存起来。
ASCII uses seven bits, so two to the power seven — one hundred and twenty-eight — different characters.
ASCII 用七个位,所以是二的七次方——一百二十八——个不同字符。
That covers upper and lower case English letters, the digits zero to nine, and the common punctuation marks.
这覆盖了英文大小写字母、零到九的数字,以及常用标点。
Unicode was built because the world needs far more than one hundred and twenty-eight symbols: other writing systems, technical signs, and emoji.
之所以有 Unicode,是因为世界需要的远不止一百二十八个符号: 其他书写系统、技术符号,还有表情符号。
Unicode uses more bits per character, so the same sentence takes more storage than pure ASCII.
Unicode 每个字符用更多的位, 所以同一句话比纯 ASCII 占的存储更多。
The trade is characters versus space.
取舍就是字符数量对存储空间。
Sound and images work the same way — measure, then store the numbers.
声音和图像的做法是一样的——先测量,再把测出来的数字存起来。
A sound wave is smooth and always changing, so the computer measures its height at regular moments.
声波是平滑而且不断变化的,所以计算机每隔固定的时间去测量一次它的高度。
That is sampling.
这就是采样。
The sample rate is how many samples are taken each second; the sample resolution is how many bits each sample uses.
采样率是每秒采样的次数;采样分辨率是每个样本用多少个位。
An image is a grid of dots called pixels.
图像则是一格一格的点组成的, 这些点叫像素。
Its resolution is how many pixels there are, and the colour depth is how many bits store each pixel's colour.
它的分辨率就是像素的数量,颜色深度就是存储每个像素颜色所用的位数。
In both cases the same trade-off appears: higher numbers give better quality, but a bigger file.
两种情况下都会出现同样的取舍:数值越高,质量越好,但文件也越大。
A sound wave is continuous, but a computer only stores numbers.
声波是连续的,但计算机只能存数字。
Sampling measures the height of the wave at regular moments — that height is the amplitude.
采样就是在固定时刻测量波的高度——那个高度叫振幅。
The sample rate is how many of those measurements happen each second, and it is measured in hertz.
采样率是每秒做多少次这样的测量,单位是赫兹。
The sample resolution is how many bits record each amplitude.
采样分辨率是记录每个振幅用多少个位。
Raise the sample rate and you capture faster wiggles in the wave; raise the resolution and you capture finer steps of loudness.
提高采样率,你能抓住波上更快的起伏;提高分辨率,你能抓住更细的响度台阶。
Both improvements make the recording more accurate, and both make the file larger.
两种改进都让录音更准确,也都会让文件更大。
That quality-versus-size trade-off is the same one you will see with images.
这种质量与大小的取舍,和图像里看到的是同一种。
A bitmap image is a grid of pixels — tiny coloured dots.
位图图像是一格一格的像素——微小的彩色点。
Resolution is simply how many pixels the grid has; a common example is one thousand nine hundred and twenty by one thousand and eighty.
分辨率就是网格里有多少个像素; 常见的例子是一千九百二十乘以一千零八十。
Colour depth is how many bits store the colour of each single pixel.
颜色深度是存储每个像素颜色所用的位数。
One bit per pixel gives only two colours; eight bits give two hundred and fifty-six; twenty-four bits give millions.
每个像素一位只能表示两种颜色;八位能表示二百五十六种;二十四位能表示上百万种。
Higher resolution means a sharper picture; higher colour depth means smoother colour.
更高的分辨率意味着更清晰的画面;更高的颜色深度意味着更平滑的色彩。
Both raise quality, and both raise file size, which is why the file-size formula multiplies width, height and colour depth together.
两者都提高质量,也都提高文件大小,所以文件大小公式要把宽、高和颜色深度乘在一起。
Storage is counted up a ladder of units that fit binary powers of two.
存储按适合二进制二的幂的单位阶梯往上数。
A bit is a single zero or one.
一个位就是单独的一个零或一。
Four bits make a nibble; eight bits make a byte.
四个位是半字节;八个位是一个字节。
From the kibibyte upward, each step is one thousand and twenty-four times the one below, because one thousand and twenty-four is two to the power ten.
从千字节往上,每一级都是下面一级的一千零二十四倍, 因为一千零二十四是二的十次方。
So one kibibyte is one thousand and twenty-four bytes; one mebibyte is one thousand and twenty-four kibibytes; then gibibyte, tebibyte, pebibyte and exbibyte follow the same rule.
所以一千字节等于一千零二十四字节; 一兆字节等于一千零二十四千字节;然后吉字节、太字节、拍字节、艾字节都遵循同样的规则。
Always convert with one thousand and twenty-four — never with a plain thousand — or the exam mark will go.
换算时永远用一千零二十四——绝不能用整一千——否则考试分数就没了。
Which brings us to file size, and two formulas the exam loves.
这就把我们带到了文件大小,以及考试特别爱考的两个公式。
For an image, the size in bits is width times height times colour depth.
对图像来说, 以位为单位的大小等于宽乘以高再乘以颜色深度。
For sound, it is the sample rate times the sample resolution times the length in seconds.
对声音来说, 等于采样率乘以采样分辨率再乘以时长秒数。
Then divide by eight for bytes, and by one thousand and twenty-four for each step up: kibibytes, then mebibytes, then gibibytes.
然后除以八得到字节, 再每往上一级除以一千零二十四:先是千字节,然后是兆字节,再是吉字节。
Try one yourself.
你自己来试一个。
Thirty seconds of sound, sampled eight thousand times a second, sixteen bits per sample.
三十秒的声音,每秒采样八千次,每个样本十六个位。
Multiply those three and you get three million, eight hundred and forty thousand bits.
把这三个数乘起来,得到三百八十四万个位。
Divide by eight: four hundred and eighty thousand bytes.
除以八:四十八万字节。
Divide again: about four hundred and sixty-nine kibibytes.
再除一次:大约四百六十九千字节。
Always divide by one thousand and twenty-four, never by a thousand.
永远除以一千零二十四,绝不能除以一千。
Now the image formula with numbers.
现在用数字把图像公式走一遍。
An image is one thousand and twenty-four by one thousand and twenty-four pixels, with a colour depth of two bytes — that is sixteen bits per pixel.
一张图像是一千零二十四乘一千零二十四个像素, 颜色深度是两个字节——也就是每个像素十六个位。
Size in bits is width times height times colour depth: one thousand and twenty-four times one thousand and twenty-four times sixteen equals sixteen million, seven hundred and seventy-seven thousand, two hundred and sixteen bits.
以位为单位的大小是宽乘高乘颜色深度: 一千零二十四乘一千零二十四乘十六,等于一千六百七十七万七千二百一十六个位。
Divide by eight for bytes: about two million and ninety-seven thousand.
除以八得到字节:大约二百零九万七千。
Divide by one thousand and twenty-four for kibibytes: two thousand and forty-eight.
再除以一千零二十四得到千字节:二千零四十八。
Divide once more: exactly two mebibytes.
再除一次:正好两兆字节。
Same ladder as sound — bits, then bytes, then kibibytes, then mebibytes.
阶梯和声音一样——位,然后字节,然后千字节,然后兆字节。
Last, compression — making a file smaller.
最后是压缩——把文件变小。
A smaller file uses less storage, needs less bandwidth, and takes less time to send.
文件小了,占用的存储更少,需要的带宽更少,传输的时间也更短。
There are two kinds.
压缩有两种。
Lossless compression makes it smaller with no permanent loss of data, so the original can be rebuilt exactly.
无损压缩把文件变小,但不会永久丢失任何数据,所以原文件可以被完整还原。
Run-length encoding is the classic example: a run of eight white pixels is stored just once, as eight W.
行程编码就是最经典的例子:连续八个白色像素,只存一次,记成"八个 W"。
Lossy compression makes it much smaller by permanently removing some data — lowering an image's resolution, or a sound's sample rate — and you cannot get it back.
有损压缩则通过永久丢掉一部分数据,把文件变得小得多——比如降低图像的分辨率, 或者降低声音的采样率——而且丢掉的再也找不回来。
So use lossless for text and program files, where every bit matters, and lossy for photos, music and video, where a little lost quality buys a much smaller file.
所以文本和程序文件要用无损压缩, 因为每一个位都重要;照片、音乐和视频用有损压缩,牺牲一点点质量,换来小得多的文件。
Run-length encoding is lossless because nothing is thrown away — only rewritten more tightly.
行程编码是无损的,因为什么都没丢掉——只是写得更紧凑。
Look at a strip of eight white pixels followed by four red ones.
看这一条:八个白色像素后面跟着四个红色像素。
Instead of storing twelve separate colour values, store a count and a value for each run: eight W, then four R.
不要存十二个单独的颜色值, 而是为每一段存一个计数和一个值:八个白,然后四个红。
When the data has long runs of the same value, the file shrinks a lot.
当数据里有很多相同值连在一起时,文件会小很多。
When every pixel is different, RLE barely helps.
如果每个像素都不同,行程编码几乎帮不上忙。
The original can still be rebuilt exactly by expanding each count back into that many copies of the value.
原图仍然可以精确还原:把每个计数展开成那么多份对应的值。
That is the definition of lossless: no permanent loss of data.
这就是无损的定义:数据没有永久丢失。
Choose compression by what you must keep.
选压缩方式,看你必须保留什么。
Text files and program code need every bit exact — a flipped bit can break a program or change a word — so use lossless methods such as run-length encoding.
文本文件和程序代码要求每一个位都准确—— 翻错一位就能弄坏程序或改掉一个词——所以要用无损方法,比如行程编码。
Photos, music and video can lose a little detail that human eyes and ears barely notice, so lossy methods win: drop resolution or colour depth on an image, or drop sample rate or sample resolution on a sound.
照片、音乐和视频可以丢掉人眼和人耳几乎注意不到的一点细节,所以有损方法更合适: 降低图像的分辨率或颜色深度,或者降低声音的采样率或采样分辨率。
The pay-off is a much smaller file, less bandwidth on the connection, and a shorter transmission time.
换来的是小得多的文件、链路上更少的带宽占用,以及更短的传输时间。
Bandwidth is simply how much data a link can carry; compression makes that limited pipe go further.
带宽就是一条链路能承载多少数据;压缩让这条有限的管道走得更远。
Four marks to lock in.
四个要拿稳的分。
First, convert to hex by grouping the binary into nibbles of four, starting from the right.
第一,转十六进制时,从右边开始把二进制每四位分成一个半字节。
Second, overflow is when the result needs more bits than the register has.
第二,溢出指的是结果需要的位数超过了寄存器所拥有的位数。
Third, for file size: width times height times colour depth for an image, and rate times resolution times seconds for sound — then divide by eight, then by one thousand and twenty-four.
第三,文件大小: 图像是宽乘高再乘颜色深度,声音是采样率乘分辨率再乘秒数——然后除以八,再除以一千零二十四。
Fourth, lossless keeps every bit, while lossy throws data away for good.
第四,无损压缩保留每一个位,而有损压缩把数据永久丢掉。
Nail these, and this topic is yours.
掌握这些,这个专题就是你的了。