A light switch has two states: on or off. Put eight switches in a row and you can make 256 different patterns, enough to give a different pattern to every English letter, digit and symbol. That's the whole idea behind how computers store data. Inside the chips are billions of tiny switches, and every photo, song, message and program is stored as patterns of on and off. This lesson explains bits, bytes and binary numbers, the language underneath everything a computer does.
You won't write binary by hand as a programmer, but you'll meet it all the time: file sizes, colours written as #FF5733, IP addresses, the reason 0.1 + 0.2 isn't exactly 0.3, and interview questions about bits. It's easier than it looks, and the only maths you need is multiplying by 2.
What you need first: CPU, RAM and storage explained.
Key terms
- Bit: the smallest unit of data, a single 0 or 1. Short for "binary digit".
- Byte: a group of 8 bits. It can hold 256 different values, from 0 to 255.
- Binary (base 2): a way of writing numbers using only the digits 0 and 1.
- Decimal (base 10): the normal way of writing numbers, with digits 0 to 9.
- Hexadecimal (base 16): a short way of writing binary, with digits 0 to 9 and A to F.
- Place value: how much a digit is worth because of its position, like the "tens" place in 42.
Why computers use 0 and 1
A computer chip is made of transistors: microscopic switches that are either on (electricity flows) or off. A modern CPU has billions of them. Two states are easy to build and very reliable to tell apart, even when electrical signals are a bit noisy. So computers store everything using two symbols: 1 for on, 0 for off.
One switch is a bit. With more bits, you get more patterns:
| Bits | Patterns | Count |
|---|---|---|
| 1 | 0, 1 | 2 |
| 2 | 00, 01, 10, 11 | 4 |
| 3 | 000 to 111 | 8 |
| 8 | 00000000 to 11111111 | 256 |
Each extra bit doubles the number of patterns. With n bits you get 2ⁿ patterns ("2 to the power n").
for bits in [1, 2, 3, 8, 16, 32]:
print(bits, "bits give", 2 ** bits, "patterns")
1 bits give 2 patterns
2 bits give 4 patterns
3 bits give 8 patterns
8 bits give 256 patterns
16 bits give 65536 patterns
32 bits give 4294967296 patterns
32 bits give about 430 crore patterns. That's why the old IPv4 internet addresses, which are 32 bits, are running out with more devices than that in the world.
How decimal place value works
Before binary, look at how you already read decimal numbers. In 472:
| Place | Hundreds (10²) | Tens (10¹) | Ones (10⁰) |
|---|---|---|---|
| Digit | 4 | 7 | 2 |
| Worth | 4 × 100 = 400 | 7 × 10 = 70 | 2 × 1 = 2 |
400 + 70 + 2 = 472. Each place is worth 10 times the place to its right, because there are 10 digits (0 to 9).
How binary place value works
Binary works the same way, but each place is worth 2 times the place to its right, because there are only 2 digits.
| Place | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Power | 2⁷ | 2⁶ | 2⁵ | 2⁴ | 2³ | 2² | 2¹ | 2⁰ |
To read a binary number, add up the place values where there's a 1.
Binary to decimal: worked example
What is 1011 in decimal?
| Place value | 8 | 4 | 2 | 1 |
|---|---|---|---|---|
| Binary digit | 1 | 0 | 1 | 1 |
| Worth | 8 | 0 | 2 | 1 |
8 + 0 + 2 + 1 = 11. Read aloud: "one zero one one in binary is eleven".
A longer one: 11001000.
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Binary digit | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
128 + 64 + 8 = 200.
Python checks both. int(text, 2) reads text as a base-2 number:
print(int("1011", 2))
print(int("11001000", 2))
11
200
Decimal to binary: worked example
To turn 45 into binary, use the subtract the biggest place value method. Go through the place values from big to small; if the place value fits into what's left, write 1 and subtract it, otherwise write 0.
| Place value | Fits into what's left? | Digit | Left over |
|---|---|---|---|
| 32 | 45 − 32 = 13 | 1 | 13 |
| 16 | No (16 > 13) | 0 | 13 |
| 8 | 13 − 8 = 5 | 1 | 5 |
| 4 | 5 − 4 = 1 | 1 | 1 |
| 2 | No (2 > 1) | 0 | 1 |
| 1 | 1 − 1 = 0 | 1 | 0 |
Reading the digits down the table: 45 is 101101 in binary. Check: 32 + 8 + 4 + 1 = 45.
There's another method you'll see in textbooks: divide by 2 repeatedly and read the remainders from bottom to top. Both give the same answer. Python's bin() does it instantly (the 0b at the front just means "this is binary"):
print(bin(45))
print(bin(200))
print(format(45, "08b")) # 8 digits, padded with zeros
0b101101
0b11001000
00101101
Counting in binary
Counting works like a car's odometer with only 0 and 1 on each wheel: when a place goes past 1, it rolls back to 0 and carries 1 to the left.
for n in range(9):
print(n, format(n, "04b"))
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
Notice the pattern: powers of two (1, 2, 4, 8) are a single 1 followed by zeros, just as powers of ten are in decimal.
Bytes
A byte is 8 bits. It's the basic unit computers use for storage and memory: file sizes, RAM and downloads are all measured in bytes. One byte can hold 256 values, 0 (00000000) to 255 (11111111). That's why you see 255 so often in computing: the brightest value in a colour, for example.
| Unit | Bytes (decimal, used by storage makers) | Bytes (binary, used by Windows) |
|---|---|---|
| Kilobyte (KB) | 1,000 | 1,024 (strictly a kibibyte, KiB) |
| Megabyte (MB) | 1,000,000 | 1,048,576 (MiB) |
| Gigabyte (GB) | 1,000,000,000 | 1,073,741,824 (GiB) |
Because computers count in powers of 2, 1,024 (2¹⁰) is the binary number closest to 1,000. The official names for the binary units are KiB, MiB and GiB, but many people and programs still say KB, MB and GB for both, which causes confusion.
Bits vs bytes in internet speeds
Internet speeds are in bits per second, with a small b: a 100 Mbps connection. File sizes are in bytes, with a big B: a 100 MB file. Since 1 byte = 8 bits, a 100 Mbps connection downloads at most about 12.5 MB per second.
speed_mbps = 100
megabytes_per_second = speed_mbps / 8
file_mb = 1500 # a 1.5 GB movie
print(megabytes_per_second, "MB/s")
print(round(file_mb / megabytes_per_second), "seconds at best")
12.5 MB/s
120 seconds at best
Hexadecimal: binary made shorter
Long binary numbers are hard to read. Hexadecimal (hex, base 16) groups bits in fours. Each group of 4 bits has 16 possible values, written as one character: 0 to 9, then A (10), B (11), C (12), D (13), E (14), F (15).
| Binary | 0000 | 0101 | 1001 | 1010 | 1111 |
|---|---|---|---|---|---|
| Hex | 0 | 5 | 9 | A | F |
| Decimal | 0 | 5 | 9 | 10 | 15 |
So one byte is always exactly two hex characters: 11001000 splits into 1100 (C) and 1000 (8), giving C8.
You'll see hex in:
- Colours on websites:
#FF5733is three bytes: red FF (255), green 57 (87), blue 33 (51). - Memory addresses in error messages, like
0x7f3a2c. - MAC addresses of network cards, like
3C:22:FB:9A:10:4E.
print(hex(200))
print(int("FF", 16), int("57", 16), int("33", 16))
0xc8
255 87 51
Where binary matters for programmers
- Data sizes: knowing that 8 bits make a byte and that each bit doubles the range helps you understand limits, such as why some old games capped values at 255.
- Colours and images: every pixel is a few bytes of colour.
- Networking: IP addresses and subnet masks are binary underneath.
- Decimal surprises: some decimals, like 0.1, can't be written exactly in binary, just as ⅓ can't be written exactly in decimal (0.333...). That's why
0.1 + 0.2gives0.30000000000000004in Python.
print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))
0.30000000000000004
0.3
Common mistakes
- Reading binary from the left with small values: the rightmost digit is the 1s place.
- Confusing Mbps and MB/s: divide by 8 to go from bits to bytes.
- Forgetting that counting starts at 0: 8 bits give 256 values, but the biggest is 255.
- Thinking hex letters are text:
AtoFare digits worth 10 to 15.
Practice
- Convert
1101and100000from binary to decimal by hand.
Show the answer
1101 = 8 + 4 + 0 + 1 = 13. 100000 = 32 (a single 1 in the 32s place). Check:
print(int("1101", 2), int("100000", 2))
13 32
- Convert 27 and 100 to binary by hand, then check with
bin().
Show the answer
27: 16 fits (11 left), 8 fits (3 left), 4 doesn't, 2 fits (1 left), 1 fits. So 11011. 100: 64 fits (36 left), 32 fits (4 left), 16 no, 8 no, 4 fits (0 left), 2 no, 1 no. So 1100100.
print(bin(27), bin(100))
0b11011 0b1100100
- How many different values can 4 bits hold? What's the biggest?
Show the answer
2⁴ = 16 values, from 0 to 15. The biggest is 1111 = 8 + 4 + 2 + 1 = 15, which is F in hex.
- Your internet plan is 40 Mbps. Roughly how long does a 600 MB file take to download, at best?
Show the answer
40 ÷ 8 = 5 MB per second, so 600 ÷ 5 = 120 seconds, or 2 minutes. Real downloads are usually a little slower.
- What colour values (0 to 255) are in the web colour
#00A86B?
Show the answer
Red 00 = 0, green A8 = 10 × 16 + 8 = 168, blue 6B = 6 × 16 + 11 = 107.
print(int("00", 16), int("A8", 16), int("6B", 16))
0 168 107
Interview questions
How many bits are in a byte, and what range of numbers can a byte store?
8 bits. An unsigned byte stores 0 to 255 (2⁸ = 256 values). A signed byte, which also stores negative numbers, stores −128 to 127.
Why do computers use binary?
Because the hardware is built from transistors that act as switches with two reliable states, on and off. Two states are simple to build, fast to switch and easy to tell apart even with electrical noise.
Why does 0.1 + 0.2 not equal 0.3 in Python?
Floats are stored in binary, and 0.1 and 0.2 have no exact binary form, just as ⅓ has no exact decimal form. The tiny rounding errors add up to 0.30000000000000004. Round for display, or use the decimal module for money.
Key takeaways
- A bit is a single 0 or 1; a byte is 8 bits and holds values from 0 to 255.
- Each extra bit doubles the number of possible values: n bits give 2ⁿ.
- Binary place values are 1, 2, 4, 8, 16, 32, 64, 128...; add up the places with a 1.
- Hexadecimal writes each group of 4 bits as one character, 0 to F.
- Internet speeds use bits (Mbps); file sizes use bytes (MB). Divide by 8.
Frequently asked questions
Is 1 KB 1,000 or 1,024 bytes?
Both are used. Storage makers and the international standard say 1 KB = 1,000 bytes. Windows and much older software use 1,024 and still call it KB; the precise name for 1,024 bytes is a kibibyte (KiB).
Do programmers need to know binary?
You need the basics in this lesson: what bits and bytes are, how to read a small binary number, and what hex is. Fields like networking, embedded systems and security use binary much more.
What does 64-bit mean for a computer?
It means the CPU works with 64-bit chunks of data and memory addresses at a time. A 64-bit system can use far more than 4 GB of RAM, which was the practical limit of 32-bit systems.
What to learn next
- See how letters, photos and songs become bytes in how text, images and sound are stored.
- Learn what manages all those bytes in what is an operating system?
- Practise with Python numbers in Python numbers and operators.