跳到主要内容

数据表示

IGCSE 计算机科学 · 第 1 主题

训练
本章视频课 打开视频页面
21:16

Why Computers Use Binary

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…

英文讲解 · 内嵌中英文字幕

1.1

计算机为何使用二进制

一台计算机只能用两个状态工作:开和关。你把这些写成 1 和 0。一个只用两个数字(digits)的系统叫二进制(binary,基数 2)。

一片蓝色的二进制数字,0 和 1
计算机把所有数据——数字、文本、声音和图像——表示为 0 和 1 的二进制串

每种数据(data)——数字、文本、声音和图像——在计算机能使用它之前都必须被变成二进制。计算机用逻辑门(logic gates)处理这个二进制,并把它存储在寄存器(registers,处理器(processor)内部的小的、快速的存储)中。

放大下的一个微处理器的硅晶片
一个微处理器容纳数百万个微小的晶体管,每个是一个开(1)或关(0)的开关——二进制的物理基础
词汇表 训练
英文 中文 拼音
digit/ˈdɪdʒɪt/ 数字 shù zì
binary/ˈbaɪnəri/ 二进制 èr jìn zhì
data/ˈdeɪtə/ 数据 shù jù
logic gate/ˈlɒdʒɪk ɡeɪt/ 逻辑门 luó jí mén
processor/ˈprəʊsesə/ 处理器 chǔ lǐ qì
register/ˈredʒɪstə/ 寄存器 jì cún qì
1.1

数制

大纲
Candidates should be able to: Notes and guidance
1 Understand how and why computers use binary to represent all forms of data • Any form of data needs to be converted to binary to be processed by a computer • Data is processed using logic gates and stored in registers
2 (a) Understand the denary, binary and hexadecimal number systems (b) Convert between (i) positive denary and positive binary (ii) positive denary and positive hexadecimal (iii) positive hexadecimal and positive binary • Denary is a base 10 system • Binary is a base 2 system • Hexadecimal is a base 16 system • Values used will be integers only • Conversions in both directions, e.g. denary to binary or binary to denary • Maximum binary number length of 16-bit
3 Understand how and why hexadecimal is used as a beneficial method of data representation • Areas within computer science that hexadecimal is used should be identified • Hexadecimal is easier for humans to understand than binary, as it is a shorter representation of binary
4 (a) Add two positive 8-bit binary integers (b) Understand the concept of overflow and why it occurs in binary addition • An overflow error will occur if the value is greater than 255 in an 8-bit register • A computer or a device has a predefined limit that it can represent or store, for example 16-bit • An overflow error occurs when a value outside this limit should be returned
5 Perform a logical binary shift on a positive 8-bit binary integer and understand the effect this has on the positive binary integer • Perform logical left shifts • Perform logical right shifts • Perform multiple shifts • Bits shifted from the end of the register are lost and zeros are shifted in at the opposite end of the register • The positive binary integer is multiplied or divided according to the shift performed • The most significant bit(s) or least significant bit(s) are lost
6 Use the two’s complement number system to represent positive and negative 8-bit binary integers • Convert a positive binary or denary integer to a two’s complement 8-bit integer and vice versa • Convert a negative binary or denary integer to a two’s complement 8-bit integer and vice versa

来源:剑桥国际大纲

用二进制计数:0 到 15

一个数制(number system)是用一组固定的数字书写数字的方式。你需要其中三个。

系统 基数 使用的数字
十进制 10 0–9
二进制 2 0 和 1
十六进制 16 0–9 然后 A–F
  • 十进制(denary)是正常的计数系统(也叫 decimal)。
  • 二进制(binary)只用 0 和 1。
  • 十六进制(hexadecimal,hex)用十六个数字:0–9,然后 A、B、C、D、E、F 代表 10、11、12、13、14、15。

基数(base)告诉你一个系统用多少个不同的数字。

Place value

一个数中的每一列有一个位值(place value)。在二进制中位值从右到左翻倍。对一个 8 位数它们是:

128  64  32  16  8  4  2  1
一个 8 位位值图,值从 128 往下到 1,150 的位放在加起来为 150 的列下面
一个 8 位位值图:1 坐在加起来为 150 的值下面

一个(bit)是一个单一的 0 或 1。八个位构成一个字节(byte)。四个位(半个字节)是一个半字节(nibble)。

Converting between number systems

十进制 → 二进制。 写出位值。在你需要的每个值下面放一个 1,使它们加起来为你的数;在其余的下面放 0。

例子:把十进制 150 变成二进制。$150 = 128 + 16 + 4 + 2$

128 64 32 16 8 4 2 1
  1  0  0  1 0 1 1 0

所以 $150$ = 10010110

二进制 → 十进制。 加上有 1 的位值。10010110 $= 128 + 16 + 4 + 2 = 150$

十六进制 → 二进制。 把每个十六进制数字变成它自己的 4 位组(一个半字节)。

例子:十六进制 F08。$F = 1111$,$0 = 0000$,$8 = 1000$,所以 F08 = 1111 0000 1000

三个十六进制数字 F、0 和 8,每个带一个箭头往下到它自己的一组四个位
每个十六进制数字映射到它自己的 4 位半字节——F08 = 1111 0000 1000

二进制 → 十六进制。 把位分成 4 个的半字节,从右边开始。把每个半字节变成一个十六进制数字。

十进制 → 十六进制。 简单的方式是先变成二进制,然后二进制变成十六进制。

这个表格帮助记十六进制字母:

十进制 二进制 十六进制
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F

Cambridge 的题目用长达 16 位的二进制数。

例题。 把十进制 100 转换成 8 位二进制,然后转换成十六进制。

$100 = 64 + 32 + 4$,所以二进制是 01100100。分成半字节,0110 0100 $= 6$$4$,所以十六进制是 64

Why hexadecimal is used

十六进制比二进制更短,对人们更容易读和写。一个十六进制数字替换 4 个二进制数字,所以你犯更少的错误。值不改变——十六进制只是显示同样的二进制的一个更短的方式。

计算机科学家把十六进制用于:

  • MAC 地址和 IPv6 地址
  • HTML 中的颜色代码(例如 #FF0000 是红色)
  • 内存地址(memory addresses)和错误代码
  • 显示内存的内容(一个"内存转储")
探索

Binary, denary and hex

Type a number and see it in binary, denary and hex — and how the place values build it.

词汇表 训练
英文 中文 拼音
number system/ˈnʌmbə ˈsɪstəm/ 数制 shù zhì
denary/ˈdiːnəri/ 十进制 shí jìn zhì
hexadecimal/ˌheksəˈdesɪml/ 十六进制 shí liù jìn zhì
base/beɪs/ 基数 jī shù
place value/pleɪs ˈvæljuː/ 位值 wèi zhí
bit/bɪt/ wèi
byte/baɪt/ 字节 zì jié
nibble/ˈnɪbl/ 半字节 bàn zì jié
memory address/ˈmeməri əˈdres/ 内存地址 nèi cún dì zhǐ
练习卷 双页
1.1

二进制加法

你可以把两个 8 位二进制数相加,从右边逐列,就像十进制。一列的规则是:

A B 结果位 进位
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1

当一个进位也进来时,$1 + 1 + 1 = 1$ 带一个 1 的进位。

例子:把 01110110(118)和 00110000(48)相加。

  0 1 1 1 0 1 1 0    (118)
+ 0 0 1 1 0 0 0 0    (48)
-------------------
  1 0 1 0 0 1 1 0    (166)
118 和 48 的 8 位二进制加法,进位位以红色显示在左列上方,给出 166
逐列相加;进位向左波动。118 + 48 = 166

Overflow

一个 8 位寄存器只能容纳 0 到 255 的十进制值。若一个加法给出一个 255 以上的结果,答案需要一个第 9 位。寄存器不能容纳这个额外的位,所以它丢失了。这叫溢出(overflow,一个溢出错误)。它在一个值超出寄存器能存储的极限时发生。

例子:11001000(200)$+$ 01001000(72)$= 272$。用二进制那是 1 00010000,它需要 9 位。前导的 1 在 8 位中放不下,所以存储的答案错误。

把 200 和 72 相加给出 272,它用二进制需要九个位;八位寄存器只保留低的八个而丢失第九位,所以存储的答案是 16 而不是 272
把 200 和 72 相加需要 9 位,但一个 8 位寄存器丢弃第九个,所以答案错误
词汇表 训练
英文 中文 拼音
overflow/ˌəʊvəˈfləʊ/ 溢出 yì chū
1.1

逻辑二进制移位

一个逻辑二进制移位(logical binary shift)把所有的位左移或右移若干位。

  • 移出寄存器末端的位丢失
  • 被加在空的末端。

一个左移每移一位把数乘以 2。一个右移每移一位把它除以 2;最右边的位(最低有效位(least significant bit(s)))丢失。

例子:把 00110101(53)左移 2 位。

start:         0 0 1 1 0 1 0 1
left shift 2:  1 1 0 1 0 1 0 0
两位的一个逻辑左移:箭头把每个位往左移两列,最左边的两位丢失,而两个零从右边进入
2 的一个左移:每个位往左移 2 位,顶部的位丢失,而零填充右边

结果是 11010100(212),它是 $53 \times 4$。最左边的两位丢失,而两个零从右边进来。若一个 1 被推出末端,那个信息就永远消失了。

词汇表 训练
英文 中文 拼音
logical binary shift/ˈlɒdʒɪkl ˈbaɪnəri ʃɪft/ 逻辑二进制移位 luó jí èr jìn zhì yí wèi
least significant bit/liːst sɪɡˈnɪfɪkənt bɪt/ 最低有效位 zuì dī yǒu xiào wèi
1.1

二进制补码

到目前为止这些数是正的。补码(two's complement)让一个 8 位寄存器也容纳负数。

在补码中,最左边的位(最高有效位(most significant bit),或 MSB)有一个的位值:

-128  64  32  16  8  4  2  1
  • 若 MSB 是 0,数是正的。
  • 若 MSB 是 1,数是负的。

要使一个正数变负: 写出正的二进制,翻转每个位(0↔1),然后加 1。

例子:制造 $-40$

  • $+40$ = 00101000
  • 翻转位 = 11010111
  • 加 1 = 11011000

所以 $-40$ = 11011000。通过加上位值检查:$-128 + 64 + 16 + 8 = -40$

一个补码 8 位图,最高有效位值 -128, 被高亮以给出 -40
最高有效位值 −128,所以 11011000 = −128 + 64 + 16 + 8 = −40

要读一个负的补码数,只需加上位值(MSB 算作 $-128$)。一个 8 位补码数的范围是 $-128$$+127$

词汇表 训练
英文 中文 拼音
two's complement/tuːz ˈkɒmplɪmənt/ 补码 bǔ mǎ
most significant bit/məʊst sɪɡˈnɪfɪkənt bɪt/ 最高有效位 zuì gāo yǒu xiào wèi
1.2

文本、声音与图像

大纲
Candidates should be able to: Notes and guidance
1 Understand how and why a computer represents text and the use of character sets, including American standard code for information interchange (ASCII) and Unicode • Text is converted to binary to be processed by a computer • Unicode allows for a greater range of characters and symbols than ASCII, including different languages and emojis • Unicode requires more bits per character than ASCII
2 Understand how and why a computer represents sound, including the effects of the sample rate and sample resolution • A sound wave is sampled for sound to be converted to binary, which is processed by a computer • The sample rate is the number of samples taken in a second • The sample resolution is the number of bits per sample • The accuracy of the recording and the file size increases as the sample rate and resolution increase
3 Understand how and why a computer represents an image, including the effects of the resolution and colour depth • An image is a series of pixels that are converted to binary, which is processed by a computer • The resolution is the number of pixels in the image • The colour depth is the number of bits used to represent each colour • The file size and quality of the image increase as the resolution and colour depth increase

来源:剑桥国际大纲

计算机通过给每个字符一个数字、然后把那个数字存储为二进制来存储文本。一台计算机能使用的字符集,连同它们的数字,是一个字符集(character set)。

  • ASCII 每个字符用 7 位,所以它有 128 个不同的字符。这对英文字母、数字和常见符号足够了。
  • Unicode 每个字符用更多的位。它能表示多得多的字符——许多语言,加上符号和表情符号(emoji)。

因为 Unicode 有更多的字符,它每个字符需要比 ASCII 更多的位,所以同样的文本占用更多的存储(storage)。

ASCII 每个字符用七位用于 128 个字符,对英文足够;Unicode 每个字符用更多的位用于多得多的字符,包括许多语言和表情符号,但占用更多的存储
ASCII 用 7 位用于 128 个字符;Unicode 用更多的位用于多得多的字符但更多的存储
词汇表 训练
英文 中文 拼音
character set/ˈkærɪktə set/ 字符集 zì fú jí
emoji/ɪˈməʊdʒi/ 表情符号 biǎo qíng fú hào
storage/ˈstɔːrɪdʒ/ 存储 cún chǔ
观看视频课 练习卷 双页
1.2

表示声音

一个声波(sound wave)平滑而一直在变。要存储它,计算机在规律的时刻测量波的高度。这叫采样(sampling),而每次测量是一个样本。

一个平滑的声波,规律的时间间隔的竖线测量它的高度,标记采样间隔和振幅
采样在规律的时刻记录波的高度(振幅)
  • 采样率(sample rate)是每秒取的样本的数目(以 Hz 测量)。
  • 采样分辨率(sample resolution)是每个样本使用的位的数目。波在一个采样点的高度是它的振幅(amplitude)。

一个更高的采样率和一个更高的采样分辨率给一个更准确的录音,但一个更大的文件。

探索

Representing sound

y = a sin(bt + c)

Sound is a wave; sampling records its height many times a second.

词汇表 训练
英文 中文 拼音
sound wave/saʊnd weɪv/ 声波 shēng bō
sampling/ˈsæmplɪŋ/ 采样 cǎi yàng
sample rate/ˈsæmpl reɪt/ 采样率 cǎi yàng lǜ
sample resolution/ˈsæmpl ˌrezəˈluːʃn/ 采样分辨率 cǎi yàng fēn biàn lǜ
amplitude/ˈæmplɪtjuːd/ 振幅 zhèn fú
resolution/ˌrezəˈluːʃn/ 分辨率 fēn biàn lǜ
1.2

表示图像

一个计算机图像由叫像素(pixels)的小点的一个网格构成。

一个 8 乘 8 的像素网格,构成一幅简单的彩色图,一个方块被标为一个像素
一个位图是一个像素网格;分辨率是它有多少像素
  • 分辨率(resolution)是图像中像素的数目(例如 $1920 \times 1080$)。
  • 颜色深度(colour depth)是用来存储每个像素颜色的位的数目。

一个更高的分辨率和一个更高的颜色深度给一个更好质量的图像,但一个更大的文件。

词汇表 训练
英文 中文 拼音
pixel/ˈpɪksl/ 像素 xiàng sù
colour depth/ˈkʌlə depθ/ 颜色深度 yán sè shēn dù
1.3

数据存储与压缩

大纲
Candidates should be able to: Notes and guidance
1 Understand how data storage is measured • Including: – bit – nibble – byte – kibibyte (KiB) – mebibyte (MiB) – gibibyte (GiB) – tebibyte (TiB) – pebibyte (PiB) – exbibyte (EiB) • The amount of the previous denomination present in the data storage size, e.g.: – 8 bits in a byte – 1024 mebibytes in a gibibyte
2 Calculate the file size of an image file and a sound file, using information given • Answers must be given in the units specified in the question. Calculations must use the measurement of 1024 and not 1000 • Information given may include: – image resolution and colour depth – sound sample rate, resolution and length of track
3 Understand the purpose of and need for data compression • Compression exists to reduce the size of the file • What the impact of this is, e.g.: – less bandwidth required – less storage space required – shorter transmission time
4 Understand how files are compressed using lossy and lossless compression methods • Lossy compression reduces the file size by permanently removing data, e.g. reducing resolution or colour depth, reducing sample rate or resolution • Lossless compression reduces the file size without permanent loss of data, e.g. run length encoding (RLE)

来源:剑桥国际大纲

数据存储用下面的单位测量。一个半字节是 4 位,一个字节是 8 位;从千字节(KiB)往上,每个单位是它之前的那个的 1024 倍(因为 $1024 = 2^{10}$,它适合二进制)。

单位 等于
bit 一个单一的 0 或 1
nibble 4 位
byte 8 位
kibibyte (KiB) 1024 字节
mebibyte (MiB) 1024 KiB
gibibyte (GiB) 1024 MiB
tebibyte (TiB) 1024 GiB
pebibyte (PiB) 1024 TiB
exbibyte (EiB) 1024 PiB
Hard-disk platters: storage is measured in bytes — knowing file size needs width × height × colour depth for images
Hard-disk platters: storage is measured in bytes — knowing file size needs width × height × colour depth for images
观看视频课 练习卷 双页
1.3

计算文件大小

图像文件大小(以位计)$=$ 分辨率 $\times$ 颜色深度 $=$$\times$$\times$ 颜色深度。

例子:一个图像是 $1024 \times 1024$ 像素,颜色深度为 2 字节($= 16$ 位)。

  • $= 1024 \times 1024 \times 16 = 16\,777\,216$
  • 字节 $= \div 8 = 2\,097\,152$ 字节
  • KiB $= \div 1024 = 2048$ KiB
  • MiB $= \div 1024 = 2$ MiB

声音文件大小(以位计)$=$ 采样率 $\times$ 采样分辨率 $\times$ 以秒计的长度。

总是除以 1024(不是 1000)以变成 KiB、MiB 等等。以题目要求的单位给出你的答案。

例题。 一个声音以 8,000 Hz 的采样率、16 位的采样分辨率被录制 30 秒。求以千字节(KiB)计的文件大小。

  • $= 8\,000 \times 16 \times 30 = 3\,840\,000$
  • 字节 $= 3\,840\,000 \div 8 = 480\,000$ 字节
  • KiB $= 480\,000 \div 1024 \approx 469$ KiB
1.3

压缩

压缩(compression)使一个文件更小。一个更小的文件:

  • 使用更少的存储空间,
  • 需要更少的带宽(bandwidth,一个连接能携带的数据量),
  • 花更短的时间发送(一个更短的传输(transmission)时间)。

有两种类型。

Lossless compression

无损(lossless)压缩使文件更小而没有永久损失数据。原始文件能被精确地重建。

一种方法是行程编码(run-length encoding,RLE)。它用值的一个副本和它重复多少次的一个计数替换一个重复值的行程。例如 WWWWWWWW(8 个白)被存储为"8 W"。这在数据有许多重复时工作得好。

一条 8 个白然后 4 个红方块的带被压缩成对 8 W 和 4 R
行程编码把每个行程作为一个计数和一个值存储一次

Lossy compression

有损(lossy)压缩通过永久移除一些数据使文件小得多。移除的数据不能取回。例如:

  • 减少一个图像的分辨率或颜色深度,
  • 减少一个声音的采样率或采样分辨率。

当你必须保留每个细节时(文本和程序文件)用无损。对照片、音乐和视频用有损,那里一个小的质量损失换来一个小得多的文件是值得的。

探索

Run-length encoding

Watch repeated symbols get squashed into a count — simple lossless compression.

词汇表 训练
英文 中文 拼音
compression/kəmˈpreʃn/ 压缩 yā suō
bandwidth/ˈbændwɪdθ/ 带宽 dài kuān
transmission/trænˈsmɪʃn/ 传输 chuán shū
lossless/ˈlɒsləs/ 无损 wú sǔn
run-length encoding/rʌn leŋθ enˈkəʊdɪŋ/ 行程编码 xíng chéng biān mǎ
lossy/ˈlɒsi/ 有损 yǒu sǔn
1.3

考试技巧

  • 通过减去位值(128、64、32 …)把十进制 → 二进制转换;通过加上有 1 的位值把二进制 → 十进制转换。
  • 要转换成十六进制,从右边把二进制分成 4 位的半字节;每个半字节恰好是一个十六进制数字。
  • 溢出在一个结果需要比寄存器有的更多的位时发生(一个 8 位寄存器只容纳 0–255),所以额外的位丢失。
  • 以位计的文件大小:对一个图像,宽 × 高 × 颜色深度;对声音,采样率 × 分辨率 × 秒。除以 8 得字节,然后每个更大的单位除以 1024。
  • 无损压缩保留每一位(文本;行程编码);有损永久移除数据(照片、音乐)以获得一个小得多的文件。

本主题的互动课程

逐步学习,并即时检测练习。

IGCSE 计算机科学历年真题

IGCSE 计算机科学的更多主题

登录或创建账号

IGCSE, A-Level & AP