Number representations and codes
Introduced| English | Meaning |
|---|---|
| binary/ˈbaɪnəri/ | A number system using base two, with the digits zero and one. |
| hexadecimal/ˌheksəˈdesɪml/ | A base-sixteen number system using digits zero to nine and letters A to F. |
| two's complement/tuːz ˈkɒmplɪmənt/ | The usual way to store negative binary integers: invert every bit and add 1. |
| range/reɪndʒ/ | The set or interval of values permitted or covered in a particular context. |
| overflow/ˌəʊvəˈfləʊ/ | A condition in which a result exceeds the range representable by the available storage or number format. |
22.5 °C, held in bits
- A thermostat shows 22.5 °C, but the microcontroller behind it holds only bits: two voltage levels, 0 and 1.
- How does 22.5 become a pattern a machine can add, compare and store? And how do negative numbers fit? This lesson is that translation.
Binary: read the weights
- In binary 二进制 each position has a weight: 128, 64, 32, 16, 8, 4, 2, 1 for 8 bits. A pattern's value is the sum of the weights under its 1s.
- To convert an integer, divide by 2 and collect the remainders, or take the largest weights that fit: $156 = 128 + 16 + 8 + 4 = 10011100_{2}$.

What is the binary number 10110 in decimal?
Difficulty 2/5 · 140 coins + a small bonus on first completion today
$16 + 4 + 2 = 22$: add the weights under the 1s.
Hexadecimal and fractions
- Hexadecimal 十六进制 is binary's shorthand: each hex digit is exactly 4 bits. Group from the right: $1001\ 1100 = \text{9C}_{16}$.
- Fractions convert by doubling and keeping the integer part: $0.625 \to 1.25\ (1) \to 0.5\ (0) \to 1.0\ (1)$, so $0.625 = 0.101_{2}$, which is $0.5 + 0.125$. The thermostat's $22.5$ is $10110.1_{2}$.
What is the hexadecimal number 3F in decimal?
Difficulty 2/5 · 140 coins + a small bonus on first completion today
$3 \times 16 + 15 = 63$. In binary it is $0011\ 1111$.
Two's complement: invert and add one
- Two's complement 二进制补码 negates a number by inverting every bit and adding 1. In 8 bits, $5 = 00000101$, so $-5 = 11111010 + 1 = 11111011$.
- Why it works: modulo $2^{N}$, the negative of $b$ is $2^{N} - b = (2^{N} - 1 - b) + 1$, and $2^{N} - 1 - b$ is $b$ with every bit flipped. So the ordinary adder also subtracts.
The range, and reading a pattern
- N bits hold the range 范围 $-2^{N-1}$ to $2^{N-1} - 1$: −128 to +127 in 8 bits. The range is lopsided: −128 exists, +128 does not.
- A leading 1 means negative. To decode, read the bits as unsigned and subtract $2^{N}$: $10011100$ is 156 unsigned, and $156 - 256 = -100$.
In 8-bit two's complement, what value do the bits 10011100 hold?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
A leading 1 means negative: $156 - 256 = -100$. Read as unsigned, the same bits are 156; the interpretation decides.
What is the most negative value an 8-bit two's complement number can hold?
Difficulty 2/5 · 140 coins + a small bonus on first completion today
$-2^{7} = -128$, the pattern $10000000$. The most positive is +127, so the range is lopsided by one.
Overflow: the sign rule
- Overflow 溢出 happens when the true result is outside the range. Detect it by sign: two positives giving a negative, or two negatives giving a positive.
- A carry out of the top bit is not, by itself, overflow. In circuit terms, overflow is when the carry into the top bit differs from the carry out.
$100 + 50 = 150 > 127$. The 8-bit adder produces $10010110$, which decodes to $-106$. Positive plus positive reading negative: overflow. The machine did not fail; the width did.
8-bit signed: 100 + 50 gives the bits 10010110. What happened?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
150 is above 127, so the result leaves the range and the bits decode to −106. The sign rule detects it.
Add the 8-bit signed values 0101 1010 (90) and 0100 0110 (70). What happens?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
The sum is $10100000$, which decodes to −96. Like signs in and an unlike sign out is overflow; there is no carry-out here at all.
Three common slips
- Reading two's complement as unsigned. $10011100$ is 156 or −100; the interpretation decides, not the bits.
- Forgetting the lopsided range. −128 fits in 8 bits; +128 does not.
- Calling every carry-out overflow. Only the sign rule (or the carry mismatch at the top bit) decides.
A carry out of the most significant bit always means signed overflow.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
For example, $-1 + 1$ in 8 bits gives a carry-out and the correct result 0. Overflow is decided by the signs, or by the carry into the top bit differing from the carry out.
Recap
- Binary value = sum of the weights under the 1s; hex = groups of 4 bits.
- Two's complement: invert and add 1; range $-2^{N-1}$ to $2^{N-1} - 1$.
- Overflow: like signs in, unlike sign out.