Grade by Grade
A. Programming Concepts and Skills · Programming Basics: Data, Decisions and Loops

How 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.

1280off641on320off160off81on41on20off11on64 + 8 + 4 + 1 = 77binary 01001101 = decimal 77
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.)

divide by 2remainder45 ÷ 2 = 22122 ÷ 2 = 11011 ÷ 2 = 515 ÷ 2 = 212 ÷ 2 = 101 ÷ 2 = 01read theremaindersfrom thebottom up45 in binary is 101101 (check: 32 + 8 + 4 + 1 = 45)
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.

binary 1101011011018+4+1 = 13D01104+2 = 66hexadecimal D6 = 13 × 16 + 6 = 214
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.
DecimalBinary (4 bits)Hex
000000
501015
910019
101010A
111011B
121100C
131101D
141110E
151111F
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 example

Example 1: Binary to decimal

Convert the byte 10110110 to decimal.

  1. Write the place values above the bits: 128, 64, 32, 16, 8, 4, 2, 1.
  2. Bits: 1 0 1 1 0 1 1 0. The 1s are under 128, 32, 16, 4 and 2.
  3. Add: 128 + 32 + 16 + 4 + 2 = 182.

10110110 in binary is 182 in decimal.

Worked example

Example 2: Decimal to binary and hex

Convert 200 to binary and to hexadecimal.

  1. 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.
  2. So the 1s are in the 128, 64 and 8 places: 11001000.
  3. Group into 4s: 1100 | 1000. 1100 = 12 = C, and 1000 = 8.
  4. Check the hex: C8 = 12 × 16 + 8 = 192 + 8 = 200. ✓

200 = 11001000 in binary = C8 in hex.

Just for fun

There 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 activity

Binary cards and finger counting

You need: 5 index cards or squares of paper and a marker.

  1. 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.
  2. Turn a card face down for 0, face up for 1. Show 13 (8 + 4 + 1, so 01101). Then show 19, 26 and 31.
  3. What is the largest number you can show with 5 cards? How many different numbers (including 0) is that?
  4. 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.
  5. Use your fingers as bits (finger up = 1). On one hand, show 6 (00110) and 21 (10101).
  6. 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

1

Convert binary 1011 to decimal.

2

Convert binary 01100100 to decimal.

3

Convert 37 to binary.

4

Convert hex 3A to decimal.

5

Convert binary 10111100 to hex.

6

How many different values can 6 bits store? What is the largest unsigned number?

7

Why do computers use binary instead of decimal?

More practice: warm-up

1

How many bits are in a byte?

2

What are the place values of the last four binary digits, from right to left?

3

Which decimal value does the hex digit E stand for?

4

Convert binary 111 to decimal.

5

What is the largest value one byte can store?

More practice: on your own

1

Convert 156 to binary, then to hex.

2

Convert hex 7E to binary and decimal.

3

A colour is written #00FF80. How much red, green and blue (0 to 255) does it have?

4

How many bits are needed to give every student in a school of 900 a different number?

5

What does print(int("1111", 2) + 0x10) display? Explain.

Challenge

1

Explain why multiplying a binary number by 2 is the same as adding a 0 on the right. Use 1011 as an example.

2

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

3

A student writes if 0.1 + 0.2 == 0.3: in a program and is surprised the condition is False. Explain, and suggest a fix.

Check your understanding

Quiz

1.

What is binary 11001 in decimal?

2.

What is 13 in binary?

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 teachers

Big 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.