Binary Numbers and Bytes
Count in binary, convert between binary and decimal, understand bits and bytes, and see why hexadecimal is a handy shorthand for programmers.
Welcome back!
Step 1 of 14Learn
Why two symbols?
Inside a computer, a wire carries either a higher voltage or a lower voltage. A switch is on or off. A spot on a disk is magnetized one way or the other. It is far easier to build electronics that reliably tell two states apart than ten, because a slightly noisy signal is still clearly high or low.
So computers store everything with two symbols: 1 and 0. One of these binary digits is a bit, the smallest unit of information. A single bit can answer one yes-or-no question: is the light on, is the box ticked, is this pixel black?
One bit is not much. But bits combine fast. Two bits give four patterns: 00, 01, 10, 11. Three bits give eight. Every extra bit doubles the number of patterns, so n bits give 2n patterns. Eight bits give 28 = 256 patterns, enough to number every letter, digit and punctuation mark on a keyboard with room to spare. A group of 8 bits is called a byte, and it is the basic unit for measuring memory and file sizes.
Step 2 of 14Explore
How many patterns can n digits make?
Change the base to compare binary (2), decimal (10) and hexadecimal (16). The curve shows how many different values x digits can represent.
- Patterns with 8 digits
- 256
- Patterns with 16 digits
- 65,536
Step 3 of 14Learn
Place value, base 2
You already know place value from decimal. In 352, the 3 means three hundreds, the 5 means five tens, the 2 means two ones. Each place is worth ten times the place to its right: 1, 10, 100, 1000.
Binary works the same way with a base of two. Each place is worth twice the place to its right: 1, 2, 4, 8, 16, 32, 64, 128 and so on.
To read 1011 in binary, line up the place values:
| 8 | 4 | 2 | 1 |
|---|---|---|---|
| 1 | 0 | 1 | 1 |
Add the values where there is a 1: 8 + 2 + 1 = 11. So binary 1011 equals decimal 11.
Going the other way, from decimal to binary, take the biggest power of two that fits, subtract it, and repeat. For 13: 8 fits (remainder 5), 4 fits (remainder 1), 2 doesn't fit, 1 fits. So 13 = 8 + 4 + 1 = 1101.
A common slip is reading binary from the left as if the leftmost digit were worth 1. The rightmost bit is always the ones place.
Step 4 of 14Practice · Chart
What each bit of 1011 is worth
Tap the place that adds nothing to the total.
Tap the bar that answers the question, then press Check.
Step 5 of 14Practice · Work it out
What is the binary number 110101 in decimal?
Step 6 of 14Practice · Work it out
Write the decimal number 25 in binary. Type only the ones and zeros.
Step 7 of 14Note
Step 8 of 14Learn
Bytes, kilobytes and the 1,000 vs 1,024 puzzle
Sizes grow in steps. In everyday use with SI prefixes:
- 1 kilobyte (KB) = 1,000 bytes
- 1 megabyte (MB) = 1,000,000 bytes
- 1 gigabyte (GB) = 1,000,000,000 bytes
- 1 terabyte (TB) = 1,000,000,000,000 bytes
Because computers work in powers of two, an older convention used 1 KB = 1,024 bytes. To avoid confusion, binary prefixes were defined: 1 kibibyte (KiB) = 1,024 bytes, 1 mebibyte (MiB) = 1,0242 bytes, 1 gibibyte (GiB) = 1,0243 bytes.
This explains a familiar surprise. You buy a 1 TB drive, plug it in, and some operating systems report about 931 GB. Nothing is missing: the drive maker counts 1012 bytes, while the operating system divides by 1,0243 and labels the result GB when it really means GiB. 1012 ÷ 1,073,741,824 ≈ 931.
Watch bits versus bytes, too. Internet speeds are quoted in megabits per second (Mbps), file sizes in megabytes. A 100 Mbps connection moves at most about 12.5 megabytes per second, because there are 8 bits in a byte.
Step 9 of 14Diagram
How big is a byte count?
Each prefix is 1,000 times the one before.
- 1 byte: one letter
1 byte
- 2 KB: a page of text
2,000 bytes
- 3 MB: a phone photo
3,000,000 bytes
- 4 GB: an HD movie
4,000,000,000 bytes
- 1 TB: a laptop drive
1,000,000,000,000 bytes
Step 10 of 14Practice · Work it out
Your internet plan is 200 Mbps (megabits per second). Ignoring overhead, how many seconds does it take to download a 500 MB (megabyte) file?
Step 11 of 14Learn
Hexadecimal: binary's shorthand
Long strings of bits are hard for humans to read. Is 1101011010110010 the same as 1101011010010010? It's hard to tell at a glance.
Programmers use hexadecimal (base 16) as a compact way to write binary. Hex needs sixteen symbols, so after 0–9 it uses A–F for ten to fifteen. The trick is that one hex digit stands for exactly four bits:
| Binary | Hex | Binary | Hex | |
|---|---|---|---|---|
| 0000 | 0 | 1000 | 8 | |
| 0011 | 3 | 1010 | A | |
| 0101 | 5 | 1100 | C | |
| 0111 | 7 | 1111 | F |
To convert, split the bits into groups of four from the right: 1101 0110 becomes D6. A byte is always exactly two hex digits, from 00 to FF (0 to 255).
You have probably seen hex already. Web colors such as #FF8800 are three bytes: red FF (255), green 88 (136), blue 00 (0), an orange. Memory addresses, error codes and network hardware (MAC) addresses are usually written in hex too.
Step 12 of 14Practice · Match the pairs
Match each binary byte to its hexadecimal form.
Tap an answer, then tap the place where it belongs (or drag it there).
- 0001 1111
- 1010 0000
- 1111 1111
- 0100 1100
Step 13 of 14Note
Step 14 of 14Practice · Choose one
A sensor stores each reading in 10 bits. How many different readings can it represent?
Lesson complete!