Bits and binary
Python for AP CS Principles Lesson 2 2:21 English narration · English + 中文 subtitles burned in
Chapters
Transcript
A bit is one digit that is either zero or one, and computers store everything — numbers, text, pictures, music — as bits.
一个"位"就是一个数字,不是 0 就是 1, 而计算机把一切——数字、文字、图片、音乐——都存成位。
The reason is physical.
理由是物理上的。
A chip is made of tiny switches, and a switch has exactly two states: off and on.
芯片是由极小的开关做成的,而一个开关恰好只有两个状态:关和开。
Call off zero and on one, and two digits fit a chip perfectly.
把关叫作 0,把开叫作 1, 两个数字就跟芯片严丝合缝。
Counting with only those two is called binary.
只用这两个数字来计数,就叫二进制。
Now read one.
现在来读一个。
Our normal numbers use ten digits; binary uses two, so each place is worth twice the place on its right — eight, four, two, one.
我们平常的数字用十个数码;二进制只用两个, 所以每一位的权重都是它右边那位的两倍——8、4、2、1。
Take one one zero one.
拿 1101 来看。
The eight counts, the four counts, the two does not, the one counts.
8 算数,4 算数,2 不算,1 算数。
Add up the ones that are lit and you get thirteen.
把亮着的那些加起来,你得到 13。
That is all reading binary is: each place is worth twice the one on its right.
读二进制就这么回事:每一位的权重是右边那位的两倍。
Python will do it for you in both directions.
Python 两个方向都能替你做。
bin of thirteen gives the string zero b one one zero one — the zero b just means "this is binary".
bin(13) 给出字符串 0b1101—— 开头那个 0b 只是说"这是二进制"。
And int of the string one one zero one, base two, reads it back as thirteen.
而 int("1101", 2) 把它读回来,得到 13。
The two functions go in opposite directions.
这两个函数走的是相反的方向。
Here is the part people skip.
下面这一段是很多人会跳过的。
A computer keeps a number in a fixed number of bits.
计算机用"固定位数"来放一个数。
Four bits give you two to the power four, which is sixteen different values — zero up to fifteen, and no further.
4 位给你 2 的 4 次方,也就是 16 个不同的值—— 从 0 到 15,再往上就没有了。
So what happens at the top?
那到了顶上会怎样?
Watch: add three to fifteen and it does not become eighteen.
看:15 加 3,它并不会变成 18。
It wraps around, back past zero, and lands on two.
它绕了回去,越过 0,落在 2 上。
This is overflow.
这就是溢出。
Four things to take with you.
带走四点。
One: a bit is one zero or one one — a switch off or on.
第一:一个位就是一个 0 或一个 1——一个开关的关或开。
Two: binary is base two, and each place is worth twice the one right.
第二:二进制是以 2 为基数,每一位的权重是右边那位的两倍。
Three: n bits store two to the power n values, from 0 upwards.
第三:n 位能存 2 的 n 次方个值,从 0 往上数。
Four: a result too big for the bits wraps round — that is overflow.
第四:装不下的结果会绕回去——那就是溢出。
Now convert some numbers in the tasks below.
现在去下面的题里做几个进制转换。