Representation catch-up: five more ways to use bits
The class moved from video to logic gates and skipped some number encodings. These three sessions of about 20 minutes each cover them in textbook order. They can open or close any logic lesson.
None of these sessions is marked as taught. Your teacher will say when each one happens.
Do this first
Sign-magnitude, one’s complement and two’s complement are in the signed numbers lesson. Do that lesson before session 2.
The three sessions
| Session | Topic | Textbook | Time | Status |
|---|---|---|---|---|
| 1 | Binary-coded decimal (BCD) | page 48 | 20 minutes | Not yet taught |
| 2 | Gray code and Excess-N | page 49 | 20 minutes | Not yet taught |
| 3 | Fixed-point and floating-point numbers | pages 50–51 | 25 minutes | Not yet taught |
Textbook: the same section, printed pages 47–51 of Computer Science for the IB Diploma (Baumgarten, Ganea and Turland, 2025). After each session, read the page shown in the table.
Session 1 · Binary-coded decimal (BCD) · 20 minutes
In BCD each decimal digit gets its own group of four bits. The number is not converted as a whole.
| Method | Working | Bits |
|---|---|---|
| BCD | 5 is 0101 and 9 is 1001 | 0101 1001 |
| Pure binary | 32 + 16 + 8 + 2 + 1 | 00111011 |
- Only the ten codes 0000 to 1001 are used. The six codes 1010 to 1111 are not valid BCD digits.
- BCD is easy to show on a digit display, such as a clock or a calculator, because each group of four bits drives one digit.
- BCD needs more bits than pure binary for the same number.
1. Write 274 in BCD.
Show answer
0010 0111 0100.
2. Decode the BCD value 1001 0011.
Show answer
93.
3. Why is 1100 not a valid BCD digit?
Show answer
1100 is 12, and a single decimal digit can only be 0 to 9.
4. How many bits are needed to store any number from 0 to 999 in BCD and in pure binary?
Show answer
BCD: three digits of four bits, so 12 bits. Pure binary: 10 bits, because 2 to the power 10 is 1024.
Session 2 · Gray code and Excess-N · 20 minutes
Gray code is an order of bit patterns in which each step changes exactly one bit.
| Number | Binary | Gray code |
|---|---|---|
| 0 | 000 | 000 |
| 1 | 001 | 001 |
| 2 | 010 | 011 |
| 3 | 011 | 010 |
| 4 | 100 | 110 |
| 5 | 101 | 111 |
| 6 | 110 | 101 |
| 7 | 111 | 100 |
Look at 3 to 4 in binary: 011 becomes 100, so three bits change at once. A sensor that reads the bits while they are changing could report a wrong number. In Gray code only one bit changes, so the reading is always the old value or the new one. Position sensors on rotating shafts use this.
To convert binary to Gray code, copy the first bit. For each later position, write 1 if that binary bit differs from the bit on its left, and 0 if they are the same. For 110: copy 1, then 1 and 1 are the same so write 0, then 1 and 0 differ so write 1. The result is 101.
Excess-N is a way to store negative numbers without a sign bit. Add a fixed number N to the value and store the result as an unsigned number. To read it back, subtract N.
| Value | Stored number | Pattern |
|---|---|---|
| −127 | 0 | 00000000 |
| −3 | 124 | 01111100 |
| 0 | 127 | 01111111 |
| 5 | 132 | 10000100 |
| 128 | 255 | 11111111 |
In Excess-N a larger value always has a larger unsigned pattern, so two stored numbers can be compared as if they were unsigned. Session 3 uses this idea.
1. Convert binary 101 to Gray code.
Show answer
Copy 1. Then 0 differs from 1, so write 1. Then 1 differs from 0, so write 1. The result is 111.
2. In the table, how many bits change between 5 and 6 in binary, and in Gray code?
Show answer
Binary: 101 to 110 changes two bits. Gray code: 111 to 101 changes one bit.
3. Store −10 in eight-bit Excess-127.
Show answer
−10 + 127 = 117, which is 01110101.
4. An Excess-127 byte holds 10000010. What value does it stand for?
Show answer
10000010 is 130, and 130 − 127 = 3.
Session 3 · Fixed-point and floating-point numbers · 25 minutes
So far every number has been a whole number. To store a fraction, we put a binary point in the pattern. Bits to the right of the point are worth a half, a quarter, an eighth and so on.
In fixed-point the binary point is always in the same agreed place. Here it sits after four bits.
| Pattern | Working | Value |
|---|---|---|
| 0101.1100 | 4 + 1 + 0.5 + 0.25 | 5.75 |
| 0011.1000 | 2 + 1 + 0.5 | 3.5 |
| 0000.0001 | one sixteenth, the smallest step | 0.0625 |
Fixed-point is simple and exact for the values it can hold, but the range and the precision are both fixed. This format cannot store a number above 15.9375 or a step smaller than 0.0625.
Floating-point lets the binary point move. It works like scientific notation: the number is stored as a sign, a mantissa that holds the digits, and an exponent that says how far to move the point.
| What to do | Result |
|---|---|
| Write it in binary | 101.11 |
| Move the point so that one digit is before it | 1.0111 × 2 to the power 2 |
| Store the three parts | sign positive, mantissa 1.0111, exponent 2 |
- The same number of bits can now hold very large and very small numbers.
- The exponent can be negative. It is commonly stored in an Excess-N form, which is why session 2 comes first.
- Many decimal fractions, such as 0.1, have no exact binary form, so the stored value is only very close.
print(0.1 + 0.2) # 0.30000000000000004
print(0.1 + 0.2 == 0.3) # False
print(0.5 + 0.25 == 0.75) # True: these have exact binary forms1. What is 0110.0100 in the four-and-four fixed-point format?
Show answer
4 + 2 + 0.25 = 6.25.
2. Write 2.625 in the same format.
Show answer
2.625 = 2 + 0.5 + 0.125, so the pattern is 0010.1010.
3. Write binary 1101.0 with one digit before the point and a power of 2.
Show answer
1.101 × 2 to the power 3.
4. Why does Python print False for 0.1 + 0.2 == 0.3?
Show answer
0.1, 0.2 and 0.3 cannot be stored exactly in binary floating-point. The small errors in 0.1 and 0.2 add up to a value that is very close to the stored 0.3 but not equal to it.