Skip to content

Number Systems & Data

A-Level Computer Science Topic 1 22:33 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
This photo. 这张照片。
This song. 这首歌。
This very video. 就连这段视频。
Your name, your messages, every file on every device — inside the computer, all of it is nothing but ones and zeros. 你的名字、你的消息、每一台设备上的每一个文件—— 在计算机内部,这一切都不过是一串串的一和零。
Just two symbols, on and off, repeated billions of times. 只有两个符号,开和关,重复上亿次。
So how does a bare string of bits become a sunset, a symphony, a sentence? 那么,一串光秃秃的比特,是怎么变成一片晚霞、一首交响乐、一句话的呢?
That is the magic of data representation. 这就是数据表示的魔法。
Underneath everything, a computer knows only numbers — and only in binary. 在这一切的底下,计算机只认识数字——而且只用二进制。
Today: binary and hexadecimal, converting between them, negative numbers in two's complement, how text, images and sound become numbers, and how we compress them. 今天:二进制与十六进制、它们之间的转换、 用补码表示负数、文字、图像和声音如何变成数字,以及我们如何压缩它们。
Let's begin. 让我们开始吧。
Before computers, an abacus did this. 在计算机出现之前,算盘就在做这件事。
Each column of beads is worth ten times the column to its right, so a few beads can mean a huge number. 每一列珠子的价值,都是它右边那一列的十倍, 所以几颗珠子就能表示一个很大的数。
That is place value, and every number system here uses it. 这就是位值,我们今天讲的每一种数制都用它。
Change how many symbols a column may hold and you change the base: ten symbols gives denary, two gives binary, sixteen gives hexadecimal. 改变一列里允许出现的符号个数,就改变了基数:十个符号是十进制,两个是二进制, 十六个是十六进制。
The idea never changes — only the base does. 想法从来没变——变的只是基数。
Humans count in base ten — ten digits, zero through nine. 人类用十进制计数——十个数字,从零到九。
A computer counts in base two: just zero and one, a single bit. 计算机用二进制计数:只有零和一,一个比特。
Eight bits make a byte. 八个比特组成一个字节。
But long binary numbers are painful to read, so we use base sixteen — hexadecimal — with sixteen digits: zero to nine, then A to F. 但长长的二进制数很难读,所以我们用十六进制——它有十六个数字: 零到九,再加上 A 到 F。
The beauty is that one hex digit stands for exactly four bits, a nibble. 妙处在于,一个十六进制数字,恰好代表四个比特,也就是一个半字节。
So hex is just binary, written short. 所以十六进制,不过是写得更短的二进制。
Hexadecimal needs sixteen digits, but after nine, we run out. 十六进制需要十六个数字,可数到九就用完了。
So it borrows letters: A stands for ten, B eleven, C twelve, D thirteen, E fourteen, and F fifteen. 于是它借来了字母: A 表示十,B 是十一,C 是十二,D 是十三,E 是十四,F 是十五。
Why sixteen? 为什么是十六?
Because four bits give exactly sixteen patterns, from all zeros to all ones. 因为四个比特恰好给出十六种组合,从全是零到全是一。
So one nibble is always one hex digit, and a whole byte is written as just two hex digits. 所以一个半字节永远对应一个十六进制数字,而一整个字节只用两个十六进制数字就写完了。
Watch what four bits can do. 看看四个比特能做到什么。
Counting up from zero, the bits roll over just like denary digits: when a column fills, it resets and carries to the left. 从零往上数,比特会像十进制数位一样翻转: 一列满了,它就归零,并向左边进位。
Four bits count from zero to fifteen — sixteen values in all. 四个比特能从零数到十五——一共十六个值。
Add a bit and you double the range: eight bits reach two hundred and fifty-five, so a byte holds two hundred and fifty-six different values. 多加一位,范围就翻一倍:八个比特能数到二百五十五, 所以一个字节可以表示二百五十六个不同的值。
Converting is all about place values. 转换的关键,全在于位值。
In binary, each column is a power of two: one, two, four, eight, and so on. 在二进制里,每一列都是二的一个幂:一、二、四、八,依此类推。
To turn two hundred into binary, find the powers of two that add up to it: one hundred and twenty-eight, plus sixty-four, plus eight. 要把二百变成二进制,就找出加起来等于它的那些二的幂:一百二十八,加六十四,加八。
Mark those columns, and you have the eight-bit pattern. 把那些列标上一,你就得到了这个八位的模式。
Then group the bits into nibbles of four, and read each as a hex digit — so two hundred becomes C eight. 然后把比特按每四个一组分成半字节, 把每一组读成一个十六进制数字——于是二百就变成了 C 八。
There are two safe ways. 有两种稳妥的做法。
First: divide by two again and again, writing the remainder each time, then read the remainders bottom-up. 第一种:不断除以二,每次记下余数,然后从下往上读这些余数。
Second, and faster in an exam: take the biggest power of two that fits and subtract. 第二种在考试里更快:取出能放进去的最大的二的幂,然后相减。
Five hundred and fifty-eight minus five hundred and twelve leaves forty-six; then thirty-two, eight, four and two finish it. 五百五十八减去五百一十二剩下四十六;接着减三十二、八、四和二,正好减完。
Mark those five columns and pad the rest with zeros — in twelve bits, that is our answer. 把这五列标上一,其余补零——写成十二位,这就是我们的答案。
Now to hex. 现在转成十六进制。
Group the bits into nibbles of four, starting from the right, and convert each one. 从右边开始,把比特按每四个一组分成半字节,再逐组转换。
Our twelve bits give two, two, E — so the answer is twenty-two E. 我们这十二位给出二、二、E——所以答案是二十二 E。
Going the other way is just as easy: swap each hex digit for its own four bits and you are straight back to binary. 反过来也一样容易: 把每个十六进制数字换成它自己的四个比特,你就直接回到了二进制。
And for denary, use the place values of hexadecimal — two hundred and fifty-six, sixteen, one — which brings us back to five hundred and fifty-eight. 要转十进制,就用十六进制的位值——二百五十六、十六、一——算下来又回到了五百五十八。
Two families of prefixes look alike but are not. 有两组前缀,看起来很像,其实不同。
In decimal, kilo means a thousand, a power of ten. 在十进制里,千表示一千,是十的幂。
In memory we count in powers of two, so kibi means one thousand and twenty-four. 而在内存里我们按二的幂计数,所以 kibi 表示一千零二十四。
The same split runs all the way up: mega and mebi, giga and gibi, tera and tebi. 这种分家一路向上都存在:兆和 mebi,吉和 gibi,太和 tebi。
That is why a one terabyte drive looks smaller once the operating system reports it in tebibytes. 这就是为什么一块一太字节的硬盘,一旦操作系统按 tebibyte 报数,看起来就变小了。
Nothing is missing — the units differ. 什么都没少——只是单位不同。
Binary addition works column by column from the right, exactly like denary. 二进制加法和十进制一模一样,从右往左,一列一列地算。
Zero plus zero is zero. 零加零得零。
Zero plus one is one. 零加一得一。
And one plus one is zero, carry the one — because two needs the next column up. 而一加一得零,向上进一——因为二需要更高的那一列。
One plus one plus a carry gives one, carry one. 一加一再加一个进位,得一,再进一。
Add these two bytes that way and you get eighty. 按这个办法把这两个字节相加,你得到八十。
Fifty-three plus twenty-seven — the same answer denary gives. 五十三加二十七——和十进制给出的答案一样。
But a register only has so many columns. 但一个寄存器只有那么多列。
Add two hundred and forty to forty-eight and the true answer, two hundred and eighty-eight, is too big to fit in eight bits. 把二百四十加上四十八,真正的答案二百八十八, 在八位里放不下。
The carry out of the leftmost column has nowhere to go. 最左边那一列进出来的位,没有地方可去。
That carry is the overflow bit, and the processor raises a flag — because the eight bits left behind are a wrong answer. 这个进位就是溢出位,处理器会因此举起一面旗子——因为留下来的那八位,是个错误的答案。
Computers do not really subtract — they turn it into an addition. 计算机其实并不做减法——它把减法变成加法。
To take thirty from one hundred, first build the two's complement of thirty: invert every bit and add one. 要从一百里减去三十, 先求出三十的补码:把每一位取反,然后加一。
Now add the two bytes as usual. 现在像平常那样把这两个字节相加。
The sum spills into a ninth bit, and here that extra carry is simply discarded. 和会溢到第九位上,而这里这个多出来的进位,直接丢掉就行。
What is left reads seventy — and one hundred minus thirty is indeed seventy. 剩下的读出来是七十——而一百减三十确实是七十。
One circuit, both operations. 一套电路,两种运算。
But how do we store a negative number, with only ones and zeros? 可是,只用零和一,我们怎么存一个负数呢?
The trick is two's complement. 诀窍就是补码。
The most significant bit — the leftmost bit — becomes a sign bit: zero for positive, one for negative. 最左边那一位,变成符号位: 零表示正,一表示负。
This is a signed integer, unlike an unsigned one. 这就是有符号整数,与无符号整数不同。
To make a number negative, invert every bit — inverting every bit — then add one. 要把一个数变成负的,就把每一位取反,然后加一。
The clever part: addition and subtraction now use the very same circuit, and there is only one zero. 妙就妙在: 加法和减法现在能用同一套电路,而且只有一个零。
In eight bits, this covers minus one hundred and twenty-eight, all the way up to plus one hundred and twenty-seven. 在八位里,这能覆盖从负一百二十八, 一直到正一百二十七。
One shift deserves its own name. 有一种移位值得单独记住它的名字。
An arithmetic shift moves every bit left or right but keeps the sign: shifting right by one place halves the value and copies the sign bit into the empty space on the left, so a negative number stays negative. 算术移位把每一位向左或向右移动,但保持符号: 右移一位相当于除以二,并把符号位复制到左边空出来的位置, 所以负数仍然是负数。
A logical shift would put a zero there instead and turn a negative number positive. 逻辑移位则会在那里补零,把一个负数变成正数。
Here is a classic exam question. 这是一道经典的考题。
What denary value is this eight-bit two's-complement number? 这个八位补码数,代表的十进制值是多少?
Start with the sign: the top bit is one, so the value is negative. 先看符号:最高位是一,所以这个值是负的。
Invert and add one to get seventy-six, then put the minus sign back — minus seventy-six. 取反加一,得到七十六,再把负号放回去——负七十六。
You can check it another way: treat the top column as minus one hundred and twenty-eight, add thirty-two, sixteen and four, and you land on the same value again. 你还可以换一种方法验算:把最高那一列当作负一百二十八, 再加上三十二、十六和四,你会又一次算出同一个值。
The sign bit cuts the range in two. 符号位把整个范围切成两半。
A top bit of zero gives the positives, nought up to one hundred and twenty-seven. 最高位是零,得到正数,从零一直到一百二十七。
A top bit of one gives the negatives, down to minus one hundred and twenty-eight. 最高位是一,得到负数,一直低到负一百二十八。
Notice that minus one is all ones, sitting just below zero. 注意负一是全一,就坐在零的下面。
The general rule for n bits: the lowest value is minus two to the power n minus one, and the highest is one less than that. 对 n 位的一般规律是: 最小值是负的二的 n 减一次幂,最大值比它小一。
To find the minimum number of bits that can store a value, ask which place values you need. 要找出存下某个值所需的最少位数,就问需要哪些位权。
An unsigned integer from zero to two to the n minus one needs n bits: two hundred needs eight bits because eight stop at two hundred and fifty-five, and a thousand needs ten. 无符号整数的范围是从零到二的 n 次方减一,需要 n 位: 二百需要八位,因为八位到二百五十五为止;一千需要十位。
A signed two's-complement integer needs n bits for the range minus two to the n minus one up to two to the n minus one, minus one — so minus two hundred needs nine bits, because eight bits stop at minus one hundred and twenty-eight. 有符号补码整数需要 n 位,范围是负的二的 n 减一次方,到二的 n 减一次方再减一—— 所以负二百需要九位,因为八位只到负一百二十八。
One hexadecimal digit needs four bits, one B C D digit four, and one ASCII character seven. 一个十六进制数字需要四位,一个 BCD 数字需要四位,一个 ASCII 字符需要七位。
Signed arithmetic overflows too, and it hides better. 有符号运算也会溢出,而且藏得更深。
Add two positive numbers, sixty-four and sixty-four, and the result reads minus one hundred and twenty-eight. 把两个正数相加,六十四加六十四, 结果读出来却是负一百二十八。
Nothing carried out of the register, yet the sign bit has flipped and the answer is wrong. 没有任何进位跑出寄存器,可符号位翻了,答案就错了。
The true value, one hundred and twenty-eight, is outside the range. 真正的值一百二十八,落在范围之外。
That is how to spot it: two positives giving a negative, or two negatives giving a positive. 这就是判断的办法: 两个正数相加得到负数,或者两个负数相加得到正数。
Before two's complement there was an older scheme: one's complement. 在补码之前,还有一种更老的方案:反码。
To make a number negative you simply invert every bit — inverting every bit — there is no add-one step. 要把一个数变成负的, 你只要把每一位取反——没有加一这一步。
So plus thirty becomes this pattern for minus thirty. 所以正三十就变成了这个表示负三十的模式。
Neat, but it has a flaw: two different zeros, a positive zero of all zeros and a negative zero of all ones. 看着挺利索,可它有个毛病:有两个不同的零,全零是正零,全一是负零。
That wastes a pattern and complicates the hardware, which is why two's complement won. 这浪费了一种组合,也让硬件更麻烦,所以最后是补码胜出。
Binary coded decimal takes a different approach. 二进码十进数走的是另一条路。
Each denary digit gets its own four bits. 每一个十进制数位,都有它自己的四个比特。
Ninety-three becomes nine, then three — one nibble each. 九十三就变成九,再加三——各占一个半字节。
That is not the same as ninety-three in pure binary, which is a single eight-bit pattern. 这和纯二进制里的九十三不一样, 那是一个单独的八位模式。
Because each nibble only ever holds nought to nine, the six patterns from ten to fifteen are invalid. 因为每个半字节只会装零到九, 十到十五这六种组合都是无效的。
Reading BCD is easy: these three nibbles say two, seven, five — two hundred and seventy-five. 读二进码十进数很容易: 这三个半字节说的是二、七、五——也就是二百七十五。
Where is BCD used? 二进码十进数用在哪里?
Anywhere a machine must show denary digits. 用在任何需要显示十进制数字的机器上。
Each digit drives one seven-segment display, so keeping the digits separate saves converting the whole number every time it changes. 每一个数位驱动一个七段显示器,把数位分开保存,就省去了每次变化都要转换整个数的麻烦。
Calculators and digital clocks work this way. 计算器和电子钟就是这样工作的。
Money code often uses BCD too, because it avoids the rounding errors you get when a fraction like nought point one is squeezed into binary. 处理货币的程序也常用它, 因为这样可以避开把零点一这种小数塞进二进制时出现的舍入误差。
So where do you actually meet hex? 那你在哪里真会碰到十六进制?
Memory addresses in low-level programming, written with a zero-x in front. 底层编程里的内存地址,前面写一个零 x。
Colour values in HTML and CSS, where two hex digits set red, two green and two blue. HTML 和 CSS 里的颜色值,两个十六进制数字定红,两个定绿,两个定蓝。
And a MAC address, six bytes identifying a network card. 还有网卡地址,六个字节标识一块网卡。
In every case the machine still stores plain binary — hex does not change the data, it only makes it readable for us. 每一种情况里,机器存的仍然是普通的二进制——十六进制不改变数据,它只是让我们更好读。
So numbers are everything — but how? 所以数字就是一切——可到底怎么做到的呢?
For text, each character gets a number: in ASCII, capital A is sixty-five. 对于文字,每个字符都得到一个数字:在 ASCII 里, 大写字母 A 是六十五。
Unicode extends this to every language and emoji. Unicode 把这扩展到每一种语言和表情符号。
For images, a bitmapped image — a bitmap — stores the colour of every pixel in a grid — more pixels, more detail. 对于图像,位图把网格里 每一个像素的颜色都存起来——像素越多,细节越丰富。
For sound, we sample the wave thousands of times a second, recording its height as a number. 对于声音,我们每秒对声波采样上千次, 把它的高度记成一个数字。
Text, pictures, sound — all just numbers underneath. 文字、图片、声音——底下全都只是数字。
Text is numbers too. 文字也是数字。
A character set gives every character a code point — a number the computer actually stores. 一个字符集给每个字符一个码点——也就是计算机真正存下来的那个数。
ASCII, the oldest common set, uses seven bits, so it has one hundred and twenty-eight code points: the basic Latin letters, the digits, punctuation, and control codes like carriage return. ASCII 是最老的通用字符集,它用七个比特,所以有一百二十八个码点: 基本的拉丁字母、数字、标点,还有像回车这样的控制码。
Extended ASCII adds an eighth bit for two hundred and fifty-six, keeping the lower half identical while the upper half changes from region to region. 扩展 ASCII 再加上第八位,凑到二百五十六个,下半部分保持完全一样, 上半部分则各地不同。
Here are four code points worth knowing. 这里有四个值得记住的码点。
Capital A is sixty-five, and lower-case a is ninety-seven — exactly thirty-two more, which is one bit's difference. 大写 A 是六十五,小写 a 是九十七—— 正好多三十二,也就是差一个比特。
The digit character zero is forty-eight, so to turn a digit character into its value you subtract forty-eight. 数字字符零是四十八, 所以要把一个数字字符变成它的数值,减去四十八就行。
And a space is thirty-two: a real character, with a real code, not an absence of one. 空格是三十二:它是一个真正的字符,有真正的编码,而不是"什么都没有"。
One hundred and twenty-eight characters cannot hold the world. 一百二十八个字符装不下整个世界。
Unicode is one universal character set covering almost every script, plus symbols and emoji. Unicode 是一个统一的字符集, 几乎涵盖每一种文字,还有符号和表情符号。
It is stored using common encodings: UTF-8 uses one to four bytes and is backwards-compatible with ASCII; UTF-16 uses two or four; UTF-32 always uses four. 它靠常见编码来存储: UTF-8 用一到四个字节,并且向下兼容 ASCII;UTF-16 用两个或四个;UTF-32 固定用四个。
Unicode wins because it represents far more characters, files stay portable with no code-page confusion, and one document can be multilingual. Unicode 胜出,是因为它能表示的字符多得多,文件可以到处通用而不会出现代码页混乱, 而且一份文档里可以有多种语言。
The trade-off: English-only files come out larger. 代价是:只含英文的文件会变得更大。
A bitmap image is a grid, and the file stores the colour of every pixel in it. 位图图像就是一个网格,文件把网格里每一个像素的颜色都存下来。
At the very front sits a file header holding the metadata — width, height and colour depth — so software knows how to read the pixel data that follows. 文件最前面是文件头,装着元数据——宽度、高度和颜色深度—— 好让软件知道后面的像素数据该怎么读。
Image resolution is the bitmap's own size in pixels, say nineteen twenty by ten eighty. 图像分辨率是位图自己的尺寸,以像素计,比如一千九百二十乘一千零八十。
Screen resolution is what the display can show. 屏幕分辨率是显示器能显示的尺寸。
Colour depth is bits per pixel: one bit gives black and white, eight gives two hundred and fifty-six colours, twenty-four gives sixteen point seven million. 颜色深度是每个像素用多少比特: 一位是黑白,八位是二百五十六色,二十四位是一千六百七十万色。
Same picture, three resolutions. 同一张图,三种分辨率。
In A the pixels are small and many, so the curve looks smooth. 在 A 里像素又小又多,所以曲线看着很平滑。
By C there are few large pixels and the disc has turned into blocks. 到了 C,像素少而大,圆盘已经变成了方块。
Nothing was redrawn — only the sampling grid got coarser. 什么都没有重画——只是采样的网格变粗了。
This is also why a low-resolution image looks blocky when it is stretched onto a bigger screen: there is no extra detail to show. 这也是为什么一张低分辨率的图,被拉大到更大的屏幕上时会显得很方: 因为根本没有多余的细节可以显示。
File size is arithmetic. 文件大小就是一道算术题。
Size in bits equals width times height times colour depth. 以比特计的大小,等于宽乘高乘颜色深度。
Take a photo three thousand by two thousand pixels at twenty-four bits per pixel: that is one hundred and forty-four million bits. 拿一张三千乘两千像素、每像素二十四位的照片:那就是一亿四千四百万比特。
Divide by eight for bytes, then by one thousand and twenty-four twice, and you get about seventeen point two mebibytes. 除以八得到字节,再除两次一千零二十四,你得到大约十七点二兆字节。
Now the trade-offs. 再看权衡。
Lower the resolution and the file shrinks but goes blocky. 降低分辨率,文件变小,但画面变方。
Lower the colour depth and it shrinks but smooth shades show banding. 降低颜色深度,文件变小,但平滑的渐变会出现色带。
Raise either and quality and size both climb. 两者中任何一个提高,画质和体积都会一起上升。
A vector graphic stores no pixels at all. 矢量图形根本不存像素。
It stores the instructions as a drawing list: an ordered set of drawing objects, each one a geometric primitive — a line, a curve, a polygon, a circle. 它存的是一张绘图列表:一组有顺序的绘图对象, 每一个都是一个几何图元——直线、曲线、多边形、圆。
Every object carries properties: its coordinates, colour, fill and line width. 每个对象都带着属性:它的坐标、颜色、填充和线宽。
Draw a house and the file says rectangle here, triangle on top, circle for the window. 画一座房子,文件里写的就是:这里一个矩形,上面一个三角形,窗户是一个圆。
To show it, the program renders the list at whatever resolution you need — so the same file suits a phone icon or a billboard. 要显示它,程序就按你需要的分辨率渲染这张列表—— 所以同一个文件既能当手机图标,也能做一张广告牌。
Enlarge both and the difference is brutal. 把两边都放大,差别就残酷了。
The bitmap diagonal becomes a staircase, because the pixels themselves get bigger. 位图的斜线变成一段一段的台阶,因为像素自己变大了。
The vector diagonal is recomputed, so it stays a clean line at any size. That decides which to use. 矢量的斜线是重新算出来的,所以不管放多大,它都还是一条干净的直线。
Vector advantage: it scales without losing quality — a vector logo stays sharp at any size. 这就决定了该用哪一种。 照片和绘画需要位图——没有哪一组形状能描述那么多细节。
Vector disadvantage: it cannot describe arbitrary pixel detail. Photographs and paintings need a bitmap. Logos, icons, signs and engineering drawings want a vector: sharp edges, exact geometry, and no loss when scaled. 标志、图标、路牌和工程图纸要用矢量:边缘锐利、几何精确,放大也不会损失。
Sound arrives as an analogue wave — smooth, continuous, with no numbers in it. 声音传来的时候是一条模拟波——平滑、连续,里面没有任何数字。
Sampling turns it digital: measure the wave's height at regular instants and write each measurement down. 采样把它变成数字的:在固定的时刻测量波的高度,把每次的测量值记下来。
Two settings control the result. 有两个设置决定结果。
The sampling rate is how many samples you take each second, in hertz; CD quality is forty-four point one kilohertz. 采样率是你每秒取多少个样本,单位是赫兹; CD 音质是四十四点一千赫。
The sampling resolution is how many bits record each sample's amplitude; CD quality is sixteen bits. 采样分辨率是用多少比特记录每个样本的振幅; CD 音质是十六位。
Sound size multiplies out too: rate times resolution times duration times channels. 声音的大小也是乘出来的:采样率乘分辨率乘时长乘声道数。
A ten-second stereo clip at CD quality is forty-four thousand one hundred, times sixteen, times ten, times two — about fourteen million bits, or one point six eight mebibytes for ten seconds. 一段十秒的 CD 音质立体声,就是四万四千一百,乘十六,乘十,乘二—— 大约一千四百万比特,也就是十秒钟一点六八兆字节。
Push the settings up and you pay for it. 把设置往上推,你是要付代价的。
A higher sampling rate captures higher pitches; a higher resolution gives finer amplitude steps and less quantisation noise. 更高的采样率能录到更高的音; 更高的分辨率让振幅的台阶更细,量化噪声更小。
Drop either and the file shrinks with audible loss. 任何一个降下来,文件变小,音质的损失也听得出来。
And the rate must be at least twice the highest frequency you want to keep. 而采样率至少要达到你想保留的最高频率的两倍。
All those numbers add up fast — so we compress them. 所有这些数字加起来增长得飞快——所以我们要压缩它们。
Lossless compression shrinks a file but keeps every bit, so it rebuilds the original exactly — perfect for text and programs. 无损压缩能缩小文件,但保留每一个比特, 所以它能一丝不差地重建出原件——非常适合文字和程序。
Lossy compression throws away detail we barely notice, for far smaller files — used for photos, music, and streaming video. 有损压缩扔掉一些我们几乎察觉不到的细节, 换来小得多的文件——用于照片、音乐和流媒体视频。
The rule: lossless when every bit matters; lossy when a little loss buys a much smaller file. 规则是:每一个比特都重要时用无损; 当一点点损失能换来小得多的文件时,就用有损。
Lossless compression has three classic methods. 无损压缩有三种经典方法。
Run-length encoding replaces a repeated value with a count: the next twelve pixels are white. 行程编码把重复的值换成一个计数: 接下来的十二个像素都是白的。
Brilliant on flat areas, useless on noise. 它在大片同色区域上极好,在杂乱数据上毫无用处。
Dictionary methods, used by ZIP and PNG, spot repeated byte sequences and store a short reference to the first copy — ideal for text and code. 字典编码被 ZIP 和 PNG 使用,它找出重复的字节序列,只存一个指向第一份的短引用—— 非常适合文字和代码。
Huffman coding gives the commonest symbols the shortest codes and rare ones longer codes, pushing the average code length down towards the data's entropy. 霍夫曼编码给最常见的符号最短的码,给罕见的符号更长的码, 把平均码长压向数据本身的熵。
Here is run-length encoding on a letter F in an eight-by-eight black-and-white grid. 这是在一个八乘八的黑白网格上,对字母 F 做行程编码。
Row by row, instead of eight separate bits we store runs: two white, four black, two white. 一行一行地看,我们不再存八个单独的比特,而是存一段一段:两个白,四个黑,两个白。
Long stretches of one colour collapse to a pair of numbers, and the image rebuilds exactly. 一长串同色就塌缩成两个数字,而图像可以一丝不差地重建出来。
On a photograph, where next-door pixels rarely match, the same trick can even make the file bigger. 可是在照片上,相邻像素很少相同,同样的招数甚至会让文件变得更大。
Lossy compression throws data away on purpose, guided by what people notice. 有损压缩是故意扔掉数据的,扔什么由人的感知决定。
JPEG drops fine detail and colour differences the eye barely registers. JPEG 扔掉眼睛几乎注意不到的细节和颜色差别。
MP3 and AAC drop pitches we hear poorly, and quiet sounds that a louder one is already masking. MP3 和 AAC 扔掉我们听得不清的音高,以及被更响的声音盖住的轻声。
Video does both, in two dimensions: spatial compression squeezes each frame like a JPEG, and temporal compression stores only what changed since the previous frame — which is why a still shot compresses so well. 视频两样都做,而且是在两个维度上:空间压缩像 JPEG 那样压每一帧, 时间压缩只存下和上一帧相比变了的部分——这就是为什么一个静止的镜头能压得那么小。
So which do you choose? 那你该选哪一种?
Use lossless whenever you need the exact data back: documents, source code, medical images. 只要你需要一模一样地取回原始数据,就用无损: 文档、源代码、医学影像。
Use lossy for streaming media, where a little loss is invisible and the saving is enormous. 流媒体用有损,那里一点点损失看不出来,而省下的空间极为可观。
Real-time video streaming has no choice — raw high-definition video is gigabytes per minute, and it must travel live over limited bandwidth. 实时视频流根本没有选择——未压缩的高清视频每分钟就有好几个吉字节, 而它必须实时穿过有限的带宽。
Lossless would not shrink it nearly enough, and the picture would keep freezing. 无损远远压不下来,画面就会一直卡住。
Three marks to secure. 三个要拿稳的分。
First, convert with place values: denary to binary by powers of two, binary to hex in nibbles of four bits. 第一,用位值来转换:十进制到二进制靠二的幂,二进制到十六进制按每四位一组的半字节。
Second, in two's complement the top bit is negative; to negate, invert and add one, and watch for overflow. 第二,在补码里最高位是负的;要取负,就取反加一,并留意溢出。
Third, know how text, images and sound become numbers, and compare lossless with lossy compression. 第三, 要懂得文字、图像和声音如何变成数字,并比较无损与有损压缩。
Master these, and data representation is yours. 掌握这些,数据表示就是你的了。
The fixed-wording definitions, one answer only. 固定措辞的定义,只给一个答案。
A bit is a single binary digit, 0 or 1; a byte is a group of eight bits. 位,是一个二进制数字,0 或者 1;字节,是八个位的一组。
A binary prefix is a multiplier that is a power of two — kibi is 1024 — rather than a power of ten. 二进制前缀,是以二的幂为倍数的乘数——kibi 是 1024——而不是十的幂。
Two's complement represents signed integers by giving the most significant bit a negative place value. 补码,通过让最高有效位具有负的位权来表示有符号整数。
Overflow is when the result of a calculation is too large to be represented in the number of bits available. 溢出,是计算结果太大,无法用可用的位数表示。
Binary coded decimal stores each denary digit as its own four-bit pattern. 二进制编码十进制,把每个十进制数字存成它自己的四位二进制模式。
A character set is the set of characters a computer can represent, each with its own binary code. 字符集,是计算机能表示的字符的集合,每个字符有自己的二进制编码。
A pixel is the smallest element of a bitmap, storing one colour value; image resolution is the number of pixels as width by height; colour depth is the number of bits storing one pixel's colour. 像素,是位图中最小的元素,存储一个颜色值; 图像分辨率,是像素数量,写成宽乘高; 颜色深度,是存储一个像素颜色所用的位数。
Sampling rate is samples per second; sampling resolution is the bits storing one sample's amplitude. 采样率,是每秒采样的次数;采样分辨率,是存储一个采样幅度所用的位数。
The traps. 陷阱。
Explain an overflow by naming the word size the question gave and saying the result cannot be represented in it — not "it was more than 255". 解释溢出时要点出题目给的字长,并说结果无法在其中表示——不要写「它大于 255」。
Make a negative by inverting every bit of the positive value and adding one; setting the top bit and leaving the rest is sign and magnitude, not two's complement. 求负数要把正数的每一位取反再加一; 只把最高位置一而其余不变,那是原码,不是补码。
Pad a converted number to the register width the question asks for. 转换后的数要补齐到题目要求的寄存器宽度。
In a file-size calculation work in bits, divide by eight once, and say whether you used 1000 or 1024. 算文件大小时统一用位,只除一次八,并且说明你用的是 1000 还是 1024。
And answer a "describe" with the syllabus terms — fewer colours, banding, lower image resolution, larger pixels — not "the picture gets worse". 另外「描述」题要用考纲的术语作答——颜色变少、出现色带、图像分辨率降低、像素变大—— 而不是「画面变差了」。
Three more marks, on the data side. 再拿三分,都在数据这一边。
Bitmap file size is width times height times colour depth, and a vector stores drawing commands instead — say that it scales without loss. 位图的文件大小是宽乘高乘颜色深度, 而矢量图存的是绘图指令——记得写上它放大不会损失质量。
For sound, quote sample rate times bit depth times time, and add channels for stereo. 对于声音,写出采样率乘位深度乘时间,立体声再乘声道数。
And when a question asks you to compare lossless with lossy compression, always give a use for each: a document for one, a video stream for the other. 而当题目让你比较无损与有损压缩时,一定各举一个用途: 一个举文档,另一个举视频流。

Log in or create account

IGCSE, A-Level & AP