Binary and data encoding
แนะนำใหม่| English | ความหมาย |
|---|---|
| unsigned/ʌnˈsaɪnd/ | unsigned |
| two's complement/tuːz ˈkɒmplɪmənt/ | A signed binary representation in which the highest bit has negative positional weight. |
| overflow/ˌəʊvəˈfləʊ/ | A condition in which a result exceeds the range representable by the available storage or number format. |
| IEEE-754 | IEEE-754 |
| rounding/ˈraʊndɪŋ/ | rounding |
| Unicode/ˈjuːnɪkəʊd/ | Unicode |
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.

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$.
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$.
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$.
- 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).