Binary and data encoding
導入済み
5:52
コンピュータシステム基礎
This course follows a program from a single bit to a running process. The first two units are the alphabet: how bits encode data, and how logic gates compute…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| byte/baɪt/ | A group of eight bits, commonly used as a unit of data storage. |
| unsigned/ʌnˈsaɪnd/ | 符号なし |
| two's complement/tuːz ˈkɒmplɪmənt/ | 2の補数 |
| overflow/ˌəʊvəˈfləʊ/ | オーバーフロー |
| IEEE-754/ˌaɪ iː iː ˈiː ˈsevənhʌndrɪdn ˈfɪfti fɔː/ | IEEE-754浮動小数点標準 |
| exponent/ekˈspəʊnənt/ | A value indicating the power to which a base is raised. |
| rounding/ˈraʊndɪŋ/ | 丸め |
| Unicode/ˈjuːnɪkəʊd/ | ユニコード |
A score that turned negative
- An old game stores the score in one byte 字节. At 127 the player scores one more point, and the screen shows −128.
- Nothing was broken. The bits did exactly what the encoding says. This lesson shows how numbers, fractions and text are stored as bits.
Binary place values
- Binary is base 2: each place is worth twice the place to its right. $10110_2 = 16 + 4 + 2 = 22$.
- With $n$ bits, an unsigned 无符号 number runs from $0$ to $2^n - 1$. One byte (8 bits) holds 0 to 255.
What is the unsigned binary number 101101 in decimal?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
32 + 8 + 4 + 1 = 45.
What is the largest unsigned number that 10 bits can hold?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
2^10 − 1 = 1024 − 1 = 1023.
Two's complement for signed numbers
- Two's complement 二进制补码 stores negative numbers: to negate, flip every bit and add 1. The 8-bit range is $-128$ to $127$.
- It wins because subtraction becomes addition: flip-and-add-1 computes $2^n - b$, which is $-b$ when we count modulo $2^n$. One adder does both jobs.

One pattern, two readings
The pattern 10110111 reads 183 unsigned and −73 in 8-bit two's complement. Change the value and see the top bit carry weight −128.
To negate a two's-complement number you —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Flip-and-add-1 computes 2^n − b, so subtraction becomes addition in one adder.
What value does the 8-bit two's-complement pattern 11111111 hold?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Read unsigned it is 255; signed, 255 − 256 = −1. Flip-and-add-1 gives 00000001, so it is −1.
Overflow
- The carry out of the top bit is always thrown away. That alone is not an error.
- Overflow 溢出 happens when two numbers of the same sign give a result of the other sign: $127 + 1$ in 8 bits gives the pattern for $-128$.
- It happens in real systems. YouTube's view counter was a 32-bit signed number, maximum 2,147,483,647, until a 2014 music video passed it. In 1996 the Ariane 5 rocket was lost about 40 seconds after launch when a 64-bit float was converted into a 16-bit integer that overflowed.
In two's-complement addition, any carry out of the top bit means the result overflowed.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The top carry is always discarded. For example −1 + 1 carries out and gives 0, which is correct. Overflow means the sign came out wrong.
Floating-point numbers
- IEEE-754 浮点标准 stores a sign, a biased exponent and a fraction. The exponent sets the scale, so floats are dense near zero and sparse far away.
- $0.1$ has no finite binary form, so it is rounded 舍入. That is why $0.1 + 0.2$ prints $0.30000000000000004$: rounding, not a bug.
0.1 + 0.2 gives 0.30000000000000004 because —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
IEEE-754 rounds the infinite binary expansion. The error is part of the design, not a defect.
Text is codes
- ASCII uses 7 bits for 128 codes. The character "1" is stored as the code 49, not the value 1.
- Unicode 统一码 gives every character a code point. UTF-8 stores each code point in 1 to 4 bytes, and plain ASCII text is already valid UTF-8.
The ASCII character 1 is stored as the number —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
It is a code, not a value: the character 1 has the code 49.
Match each encoding to what it stores.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
The same bits mean different things; the encoding says how to read them.
Worked example: −73 and an overflow
- $-73$ in 8 bits: $73 = 01001001_2$, flip to $10110110$, add 1 to get $10110111$.
- $96 + 64 = 160 = 10100000_2$. Unsigned 无符号, that is fine. Signed, 160 is above 127, so the pattern reads $160 - 256 = -96$.
Worked example: decode a float
- Which number is stored in the 32-bit float
0 10000001 01000000000000000000000?
| Part | Bits | Means |
|---|---|---|
| Sign | 0 |
positive |
| Exponent 指数 | 10000001 |
129 − 127 = 2 |
| Fraction | 0100… |
1.25 |
-
The exponent is stored with a bias of 127, and the fraction has an implied leading 1.
-
So the value is $+1.25 \times 2^{2} = \mathbf{5.0}$. Check: $5 = 101_2 = 1.01_2 \times 2^{2}$.
A 32-bit float has sign 0, exponent bits 10000010 and fraction bits 1000… (all the rest 0). What number does it store?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Exponent 130 − 127 = 3; fraction 1.1 in binary = 1.5; so 1.5 × 2³ = 12.
Three common slips
- Thinking a pattern has one value. $10110111$ is 183 unsigned 无符号 and $-73$ signed: the type decides.
- Testing floats with
==after arithmetic. Compare with a small tolerance instead. - Mixing up a character and a number. "1" is code 49.
Recap
- Unsigned $0$ to $2^n - 1$; two's complement 二进制补码: flip and add 1, range $-2^{n-1}$ to $2^{n-1} - 1$. In 8 bits:
| Pattern | Unsigned | Two's complement |
|---|---|---|
01111111 |
127 | 127 |
10000000 |
128 | −128 |
11111111 |
255 | −1 |
- The top carry is discarded; overflow means the sign came out wrong.
- Floats round; text is codes (ASCII 7 bits, Unicode code points, UTF-8 1–4 bytes).
何が問題でしたか?この内容はサイトの運営チームだけが確認します。