A. Programming Concepts and Skills · Programming Basics: Data, Decisions and LoopsHow Computers Store Numbers: Binary and Hexadecimal
A1.2About 70 minutes
Learning goals- I can explain why computers store all data as bits (0s and 1s).
- I can convert between binary and decimal numbers.
- I can convert between binary, hexadecimal and decimal numbers.
- I can work out how many values a given number of bits can represent.
- I can explain why some decimal numbers, such as 0.1, cannot be stored exactly as a float.
Words to know
bit
A binary digit: a single 0 or 1. The smallest unit of data a computer stores.
byte
A group of 8 bits. One byte can store 256 different values (0 to 255).
binary (base 2)
A number system with only the digits 0 and 1. Each place is worth a power of 2.
decimal (base 10)
The everyday number system with digits 0 to 9. Each place is worth a power of 10.
hexadecimal (base 16)
A number system with 16 digits: 0 to 9 and A (10) to F (15). Each hex digit stands for 4 bits.
place value
The amount a digit is worth because of its position. In binary the places are 1, 2, 4, 8, 16, ...
Everything is 0s and 1s
Your photos, your playlist, this lesson and every Python program you write are all stored the same way: as long rows of 0s and 1s. Why?
A computer's memory and processor are built from billions of tiny electronic switches called transistors. A switch is either off or on. It is easy to build circuits that tell two states apart reliably, even when there is a little electrical noise. So computers use a number system with only two digits: binary. One binary digit is a bit. Eight bits make a byte.
Expectation A1.2 asks you to understand how a computer uses binary, hexadecimal, ASCII and Unicode to represent data. This lesson covers numbers; the next lesson covers text.
Place value, base 10 and base 2
In decimal, the number 307 means 3 × 100 + 0 × 10 + 7 × 1. The places are powers of 10: 1, 10, 100, 1000, ...
Binary works the same way, but the places are powers of 2: 1, 2, 4, 8, 16, 32, 64, 128, ... (from right to left). Each digit is 0 or 1, so a place is either counted or not counted.
To convert binary to decimal, add the place values wherever there is a 1.
One byte is 8 bits. Each position is worth a power of 2. Add the place values of the bits that are 1 (switched on): 64 + 8 + 4 + 1 = 77.Decimal to binary
There are two common methods.
Method 1: subtract powers of 2. Find the largest power of 2 that fits, write a 1 there, subtract, and repeat with what is left. For 77: 64 fits (13 left), 32 does not, 16 does not, 8 fits (5 left), 4 fits (1 left), 2 does not, 1 fits. So 77 = 1001101.
Method 2: divide by 2 repeatedly. Divide by 2 and write down the remainder (0 or 1). Keep dividing the quotient until it reaches 0. Read the remainders from the bottom up. (This uses the // and % you learned in Lesson 4.)
Converting decimal 45 to binary by repeatedly dividing by 2. The last remainder is the first (leftmost) bit.How many values fit in n bits?
Each extra bit doubles the number of patterns:
- 1 bit: 2 patterns (0, 1)
- 2 bits: 4 patterns (00, 01, 10, 11)
- 8 bits (1 byte): 2⁸ = 256 patterns, so the values 0 to 255
- 16 bits: 2¹⁶ = 65 536 patterns
In general, n bits can store 2ⁿ different values. The largest unsigned whole number in n bits is 2ⁿ − 1.
Storage is measured in bytes: a kilobyte (kB) is 1000 bytes, a megabyte (MB) is 1 000 000 bytes and a gigabyte (GB) is 1 000 000 000 bytes. (Some programs use 1024 instead of 1000; the units for those are KiB, MiB and GiB.)
Hexadecimal: a short form for binary
Long rows of bits are hard for people to read. Hexadecimal (hex, base 16) is a compact way to write them. It needs 16 digits, so after 0 to 9 it uses letters: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.
Because 16 = 2⁴, one hex digit is exactly 4 bits. A byte is always 2 hex digits, from 00 to FF (255).
- Binary to hex: split the bits into groups of 4 from the right, and convert each group.
- Hex to decimal: the places are 1, 16, 256, ... So
2F = 2 × 16 + 15 = 47.
You see hex every day without noticing: web colours like #FF0000 (red) use one byte (two hex digits) each for red, green and blue. Network card (MAC) addresses and error codes are also written in hex.
Every hexadecimal digit stands for exactly 4 bits. Split the byte into two groups of 4, convert each group, and write the hex digits side by side.| Decimal | Binary (4 bits) | Hex |
|---|
| 0 | 0000 | 0 |
| 5 | 0101 | 5 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
Useful values to know. Every 4-bit group matches exactly one hex digit.Binary and hex in Python
Python can write and convert these number systems for you, which is handy for checking your work:
0b1011 is a binary literal (11) and 0xFF is a hex literal (255).bin(77) gives the string '0b1001101'; hex(214) gives '0xd6'.int("1011", 2) reads a string as base 2; int("D6", 16) reads it as base 16.
print(0b1011, 0xFF)
print(bin(77), hex(214))
print(int("10110110", 2), int("2F", 16))
Use these to check hand conversions. On tests you will be expected to convert by hand. Output, line by line: 11 255 | 0b1001101 0xd6 | 182 47
Why 0.1 + 0.2 is not exactly 0.3
Fractions work in binary too, with places worth 1/2, 1/4, 1/8, and so on. Some decimal fractions, like 0.5 (= 1/2) and 0.25, have exact binary forms. But 0.1 has no exact binary form, just as 1/3 has no exact decimal form (0.3333...). A float stores a very close approximation, so tiny errors appear:
print(0.1 + 0.2) shows 0.30000000000000004.
This is not a Python bug; almost every language does this. It is why you round floats for display, and why programs that handle money often store whole cents as integers.
Worked exampleExample 1: Binary to decimal
Convert the byte 10110110 to decimal.
- Write the place values above the bits: 128, 64, 32, 16, 8, 4, 2, 1.
- Bits: 1 0 1 1 0 1 1 0. The 1s are under 128, 32, 16, 4 and 2.
- Add: 128 + 32 + 16 + 4 + 2 = 182.
10110110 in binary is 182 in decimal.
Worked exampleExample 2: Decimal to binary and hex
Convert 200 to binary and to hexadecimal.
- Subtract powers of 2: 200 − 128 = 72; 72 − 64 = 8; 32 and 16 do not fit; 8 − 8 = 0; 4, 2 and 1 do not fit.
- So the 1s are in the 128, 64 and 8 places: 11001000.
- Group into 4s: 1100 | 1000. 1100 = 12 = C, and 1000 = 8.
- Check the hex: C8 = 12 × 16 + 8 = 192 + 8 = 200. ✓
200 = 11001000 in binary = C8 in hex.
Just for funThere are 10 kinds of people in the world: those who understand binary, and those who don't.
(If that joke did not make sense yet, read 10 as a binary number.)
Hands-on activityBinary cards and finger counting
You need: 5 index cards or squares of paper and a marker.
- On the cards, draw 1, 2, 4, 8 and 16 dots. Lay them in a row from 16 on the left to 1 on the right.
- Turn a card face down for 0, face up for 1. Show 13 (8 + 4 + 1, so 01101). Then show 19, 26 and 31.
- What is the largest number you can show with 5 cards? How many different numbers (including 0) is that?
- Count from 0 to 10 by flipping cards. Notice that the 1-card flips every time, the 2-card every second time, and so on.
- Use your fingers as bits (finger up = 1). On one hand, show 6 (00110) and 21 (10101).
- Check your answers in Python with
bin().
What to look for: 19 = 10011, 26 = 11010, 31 = 11111. Five cards can show 32 numbers, 0 to 31 (2⁵ = 32). With ten fingers you could count from 0 to 1023.
Your turn
1Convert binary 1011 to decimal.
2Convert binary 01100100 to decimal.
4Convert hex 3A to decimal.
5Convert binary 10111100 to hex.
1011 | 1100 = B | C, so BC.
6How many different values can 6 bits store? What is the largest unsigned number?
2⁶ = 64 values; the largest is 63.
7Why do computers use binary instead of decimal?
Their circuits are built from switches (transistors) that are either off or on. Two states are easy to build and to tell apart reliably, so each digit is 0 or 1.
More practice: warm-up
1How many bits are in a byte?
2What are the place values of the last four binary digits, from right to left?
3Which decimal value does the hex digit E stand for?
4Convert binary 111 to decimal.
5What is the largest value one byte can store?
More practice: on your own
1Convert 156 to binary, then to hex.
156 = 128 + 16 + 8 + 4 → 10011100. Groups 1001 | 1100 = 9 | C, so 9C. Check: 9 × 16 + 12 = 156.
2Convert hex 7E to binary and decimal.
7 = 0111, E = 1110, so 01111110. Decimal: 7 × 16 + 14 = 126.
3A colour is written #00FF80. How much red, green and blue (0 to 255) does it have?
Red 00 = 0, green FF = 255, blue 80 = 8 × 16 = 128. So 0 red, 255 green, 128 blue.
4How many bits are needed to give every student in a school of 900 a different number?
2⁹ = 512 is too few; 2¹⁰ = 1024 is enough. So 10 bits.
5What does print(int("1111", 2) + 0x10) display? Explain.
31. int("1111", 2) is 15 and 0x10 is 16; 15 + 16 = 31.
Challenge
1Explain why multiplying a binary number by 2 is the same as adding a 0 on the right. Use 1011 as an example.
Adding a 0 on the right moves every bit one place left, so each bit is worth twice as much. 1011 = 11 and 10110 = 16 + 4 + 2 = 22 = 2 × 11. (It is like adding a 0 to multiply by 10 in decimal.)
2Each pixel of a colour image uses 3 bytes (red, green, blue). Estimate the size of an uncompressed 1920 × 1080 image in megabytes (1 MB = 1 000 000 bytes).
1920 × 1080 = 2 073 600 pixels. × 3 bytes = 6 220 800 bytes ≈ 6.2 MB. (Real photo files are smaller because formats like JPEG compress the data.)
3A student writes if 0.1 + 0.2 == 0.3: in a program and is surprised the condition is False. Explain, and suggest a fix.
0.1 and 0.2 cannot be stored exactly in binary, so their sum is 0.30000000000000004, which is not equal to the float closest to 0.3. Fix: compare rounded values, round(0.1 + 0.2, 2) == 0.3, or check that the difference is tiny, for example less than 0.000001 (Lesson 8 covers comparisons).
Check your understandingQuiz
1. What is binary 11001 in decimal?
3. How many hex digits are needed to write one byte?
4. What is hex FF in decimal?
5. How many different values can 10 bits represent?
6. Why does 0.1 + 0.2 print 0.30000000000000004?
For parents and teachersBig idea: expectation A1.2 asks students to understand how a computer uses binary and hexadecimal (and ASCII and Unicode, next lesson) to represent data internally.
How to help: the five binary cards make a quick quiz game: call out a number from 0 to 31 and have your teen flip the cards to show it. Paint colour pickers in many apps show hex codes like #1E90FF; ask them to read off the red, green and blue values.
Common difficulties:
- Reading place values from the wrong end. The ones place is always on the right.
- Reading the division-method remainders top-down instead of bottom-up.
- Forgetting that A to F stand for 10 to 15.
- When grouping bits into 4s for hex, start from the right and add leading zeros on the left if needed.