---
title: Powers of two and binary prefixes
heading: Why a 1 TB drive shows up as 931 GB
eyebrow: A1 · Lesson 3 · Powers of two
xp: 40
---

::: hook
Plug a brand-new 1 TB drive into a computer and it reports 931 GB. Nobody stole 69 gigabytes. The drive maker and the operating system are counting in different units that happen to share a name. One counts in powers of ten, like every other SI prefix. The other counts in powers of two, because that's how digital hardware addresses memory. An engineer who works with ADCs, memory chips or network links has to keep the two straight.

::: play
- The solid curve is $2^N$, the number of different values $N$ bits can hold. The dashed curve is $10^{0.3N}$, the rule of thumb that ten bits make about a thousand.
- Slide $N$ to 10, 20, 30 and 40 and compare the two counts in the readouts.
- Watch the ratio. Does the rule of thumb get better or worse as $N$ grows?
:::
:::

```sim plot
view: graph
x: N = 0..40
y: values = 1..10T log
slider: n = 1..40 step 1 = 10
plot: 2^N | label: 2ᴺ
plot: 10^(0.3*N) | dashed | color: ch2 | label: 10^(0.3N)
point: n, 2^n | label: N bits
readout: 2ᴺ = 2^n | format: plain
readout: 10^(0.3N) = 10^(0.3*n) | format: plain
readout: 2ᴺ ÷ 10^(0.3N) = 2^n/10^(0.3*n) | digits: 4
```

::: predict
## A drive holds exactly $10^{12}$ bytes. The operating system divides by $2^{30}$ and calls the result GB. What does it show?

- [ ] 1000 GB
- [ ] 1024 GB
- [x] 931 GB
- [ ] 976 GB

::: reveal
It shows 931 GB. $10^{12} / 2^{30} = 10^{12} / 1\,073\,741\,824 = 931.3$. The two kinds of giga differ by 7.4%, and the gap grows with every prefix step, as the ratio in the sim shows: 2.4% at kilo, 4.9% at mega, 7.4% at giga and 10% at tera.
:::
:::

::: nameit
## Bits, bytes and the prefixes that count in twos

A **bit** holds one of two values, 0 or 1. Each extra bit doubles the number of patterns, so

$$N \text{ bits can hold } 2^{N} \text{ different values}$$

Counting from zero, those values run from 0 to $2^N - 1$: an 8-bit number goes from 0 to 255, a 16-bit number from 0 to 65 535. A **byte** is 8 bits.

The number that makes powers of two easy to estimate is

$$2^{10} = 1024 \approx 10^{3}$$

so every ten bits multiply the count by about a thousand: $2^{20}$ is about a million, $2^{30}$ about a billion. Going the other way, one decimal digit is worth about 3.3 bits, since $\log_2 10 = 3.32$.

Memory is built from address lines, and $N$ address lines pick out one of $2^N$ locations. So memory sizes come in powers of two, and people started calling 1024 bytes a "kilobyte" because it was close enough. In 1998 the IEC gave the binary multiples their own names, so there's no longer any need to guess:

| Prefix | Symbol | Value | Compare |
|---|---|---|---|
| kibi | Ki | $2^{10}$ = 1 024 | kilo, $10^3$ |
| mebi | Mi | $2^{20}$ = 1 048 576 | mega, $10^6$ |
| gibi | Gi | $2^{30}$ = 1 073 741 824 | giga, $10^9$ |
| tebi | Ti | $2^{40}$ ≈ 1.0995 × 10¹² | tera, $10^{12}$ |

In practice:
- **Drives** are sold in decimal units: 1 TB is $10^{12}$ bytes.
- **Memory chips and RAM** are sized in binary units. A "16 GB" laptop has 16 GiB, $16 \times 2^{30}$ bytes; memory datasheets have long used the plain prefixes with binary meanings.
- **Operating systems** differ: some show GiB with the label GB, which is where the missing 69 GB went.
- **Data rates** are decimal and usually in **bits** per second: a 100 Mb/s link moves $10^8$ bits, or 12.5 MB, each second. Watch the lowercase b.

**How many bits do you need?** To give $M$ things their own number, you need the smallest $N$ with $2^N \ge M$, which is $N = \lceil \log_2 M \rceil$. With 300 sensors, 8 bits give only 256 addresses, so you need 9.

**An ADC's step.** An analog-to-digital converter with $N$ bits splits its input range into $2^N$ steps. A 12-bit converter reading 0 to 3.3 V has steps of $3.3 / 4096 = 0.806$ mV. That one step is the smallest change it can see, often called one LSB (least significant bit).
:::

::: example
## A pressure sensor puts out 0 to 5 V, and you need to see changes of 1 mV. How many bits does the ADC need?

::: solution
1. **Count the steps needed.** The range has to split into steps no bigger than 1 mV, so you need at least
   $$\frac{5\ \text{V}}{1\ \text{mV}} = 5000\ \text{steps}$$
2. **Try 12 bits.** That's too few:
   $$2^{12} = 4096\ \text{steps}$$
3. **Try 13 bits.** $2^{13} = 8192$ steps is enough, with steps of
   $$\frac{5\ \text{V}}{8192} = 0.61\ \text{mV}$$
4. **Pick a real part.** Few converters come with 13 bits. The common sizes are 10, 12, 14 and 16, so in practice you'd pick a 14-bit part, with steps of
   $$\frac{5\ \text{V}}{2^{14}} = 0.31\ \text{mV}$$

**Answer:** it needs at least 13 bits (steps of 0.61 mV). In practice you'd use a 14-bit converter, with steps of 0.31 mV.
:::
:::

::: example
## How long does a 4 GiB file take to copy over a 100 Mb/s link?

::: solution
1. **The file in bytes.** A GiB is $2^{30}$ bytes:
   $$\begin{aligned} 4\ \text{GiB} &= 4 \times 2^{30}\ \text{bytes} \\ &= 4\,294\,967\,296\ \text{bytes} \end{aligned}$$
2. **The file in bits.** Each byte is 8 bits, so it's 34 359 738 368 bits, about $3.44 \times 10^{10}$.
3. **Copy time.** The link carries $10^8$ bits each second:
   $$\begin{aligned} t &= \frac{3.44 \times 10^{10}}{10^8}\ \text{s} \\ &= 344\ \text{s} \end{aligned}$$
   a little under 6 minutes.
4. **Watch the units.** The traps are the binary GiB on the file and the lowercase b on the link. Forgetting that a byte is 8 bits would make the answer eight times too small.
5. **Allow for overhead.** A real copy takes a little longer, because the network adds headers to every packet.

**Answer:** about 344 s, a little under 6 minutes.
:::
:::

::: example
## A memory chip has 17 address lines and stores one byte at each address. How big is it?

::: solution
1. **Count the addresses.** Seventeen lines pick out
   $$2^{17} = 131\,072\ \text{addresses}$$
   so the chip holds 131 072 bytes.
2. **Binary units.**
   $$\begin{aligned} \frac{2^{17}}{2^{10}} &= 2^{7} \\ &= 128\ \text{KiB} \end{aligned}$$
   a round number, which is why memory comes in sizes like this.
3. **Decimal units.** In decimal units it's 131.1 kB.

**Answer:** the chip holds 131 072 bytes: 128 KiB, or 131.1 kB.
:::
:::

::: check
given: N = 5..16 int
answer: 2^N
tolerance: 0.5

## How many different values can a number with {N} bits hold?

::: right
Right. Each bit doubles the count, so {N} bits give $2^{\given{N}}$ = {answer} values.
:::

::: misses
- [2*N] That adds two for each bit. Each bit doubles the count instead, so it's 2 multiplied by itself {N} times.
- [N^2] That's {N} squared. The count is 2 to the power {N}.
- [2^N - 1] That's the largest value, counting up from 0. Count the 0 too.
- [any] Not yet. Each extra bit doubles the number of patterns.
:::
:::

::: check
given: k = 4 | 8 | 16 | 32 | 64 | 128 | 256
answer: k*1024
tolerance: 0.5

## A microcontroller has {k} KiB of RAM. How many bytes is that?

::: right
Right. One KiB is 1024 bytes, so {k} KiB is {answer} bytes.
:::

::: misses
- [k*1000] That's {k} kB, in decimal kilobytes. A kibibyte is $2^{10}$ = 1024 bytes.
- [k*1024*8] That's the number of bits. A byte is 8 bits, so leave out the 8.
- [any] Not yet. Multiply by 1024, the value of one Ki.
:::
:::

::: check
given: T = 1 | 2 | 4 | 8 | 16 | 20
answer: T*1e12/2^30
tolerance: 0.2%

## A drive sold as {T} TB is plugged into a computer that counts in GiB. How many GiB does it show?

::: right
Right. The drive holds $\given{T} \times 10^{12}$ bytes, and one GiB is $2^{30}$ bytes: {answer} GiB.
:::

::: misses
- [T*1000] That's how many decimal GB the maker counts. The computer divides by $2^{30}$ = 1 073 741 824 instead of $10^9$.
- [T*1024] That's how many GiB there are in {T} TiB. The drive was sold in decimal terabytes, $10^{12}$ bytes each.
- [any] Not yet. Write the size in bytes, then divide by $2^{30}$.
:::
:::

::: check
given: r = 20 | 50 | 100 | 250 | 500; S = 100 | 250 | 500 | 750
answer: S*8/r
tolerance: 1%

## A download runs at {r} Mb/s. How many seconds does a {S} MB file take? Take M as $10^6$ in both, and ignore the protocol's overhead.

::: right
Right. The file is $\given{S} \times 8$ megabits, and at {r} megabits each second that takes {answer} s.
:::

::: misses
- [S/r] Check the b's: the rate is in bits and the file is in bytes. Each byte is 8 bits.
- [S/(8*r)] Each byte is 8 bits, so the file is 8 times more bits than bytes. Multiply by 8.
- [any] Not yet. Convert the file to megabits first.
:::
:::

::: check
given: M = 300 | 1000 | 5000 | 20000 | 70000 | 1000000
answer: ceil(ln(M)/ln(2))
tolerance: 0.5

## A building has {M} sensors on one network, and each needs its own address. What is the smallest number of address bits that will do?

::: right
Right. $2^{\given{answer}}$ is the first power of two that reaches {M}, so {answer} bits will do and one fewer won't.
:::

::: misses
- [floor(ln(M)/ln(2))] One short: 2 to that power is less than {M}. Round the number of bits up.
- [ceil(log10(M))] That's how many decimal digits {M} has. Each bit only doubles, so you need more bits than digits.
- [any] Not yet. Find the smallest N with $2^N$ at least {M}.
:::
:::

::: check
given: N = 8 | 10 | 12 | 16; V = 1.8 | 3.3 | 5
answer: V/2^N
tolerance: 0.5%

## An ADC with {N} bits measures inputs from 0 to {V} V. What voltage does one step stand for? Use $2^N$ steps.

::: right
Right. {V} V split into $2^{\given{N}}$ steps is {answer} V a step.
:::

::: misses
- [V/N] That splits the range into {N} parts, one per bit. {N} bits make $2^{\given{N}}$ steps.
- [V*2^N] Divide the range by the number of steps.
- [any] Not yet. Divide {V} V by $2^{\given{N}}$.
:::
:::

::: check
kind: choice

## A laptop is listed with 16 GB of RAM and a 512 GB drive. Which is right?

- [ ] Both are counted in powers of ten
- [ ] Both are counted in powers of two
- [x] The RAM is $16 \times 2^{30}$ bytes and the drive is $512 \times 10^{9}$ bytes
- [ ] The RAM is $16 \times 10^{9}$ bytes and the drive is $512 \times 2^{30}$ bytes

::: right
Right. Memory is built in powers of two because of its address lines, and memory makers use GB to mean $2^{30}$. Drive makers use the SI meaning, $10^9$.
:::

::: misses
- [any] Think about which part is addressed by binary address lines. Its size is a power of two.
:::
:::

::: explain
## Why do memory sizes come in powers of two, while drive sizes don't have to?

::: model
A memory chip picks out a location with binary address lines, and $N$ lines can name exactly $2^N$ locations. A chip built with a size that isn't a power of two would leave some addresses pointing at nothing, so memory comes in powers of two. A drive is addressed by sector numbers that can count to any total, so its maker is free to sell round decimal sizes.
:::
:::

::: practice

::: check
given: N = 5..16 int
answer: 2^N - 1
tolerance: 0.5

## What is the largest value an unsigned {N}-bit number can hold?

::: misses
- [2^N] That's how many values there are. Counting from 0, the last one is one less.
- [any] {N} bits hold $2^{\given{N}}$ values, starting at 0.
:::
:::

::: check
given: k = 1 | 2 | 4 | 8 | 16 | 32 | 64
answer: k*2^20
tolerance: 0.5

## How many bytes is {k} MiB?

::: misses
- [k*1e6] That's {k} decimal MB. A mebibyte is $2^{20}$ = 1 048 576 bytes.
- [k*1024] That's {k} KiB. A MiB is 1024 KiB.
- [any] One MiB is $2^{20}$ bytes.
:::
:::

::: check
given: T = 1 | 2 | 4 | 8 | 12 | 18
answer: T*1e12/2^40
tolerance: 0.2%

## How many TiB is a {T} TB drive?

::: misses
- [T] The two teras differ: one TiB is $2^{40} \approx 1.0995 \times 10^{12}$ bytes.
- [T*2^40/1e12] That's upside down. A TiB is bigger than a TB, so the answer is a bit less than {T}.
- [any] Write it in bytes, $\given{T} \times 10^{12}$, then divide by $2^{40}$.
:::
:::

::: check
given: r = 10 | 25 | 100 | 400 | 1000
answer: r/8
tolerance: 0.5%

## A link runs at {r} Mb/s. How many MB does it move each second?

::: misses
- [r*8] A byte is 8 bits, so there are fewer bytes than bits. Divide by 8.
- [r] Mb is megabits and MB is megabytes. Divide by 8.
- [any] Divide the bits by 8 to get bytes.
:::
:::

::: check
given: M = 100 | 500 | 2000 | 4000 | 50000 | 300000
answer: ceil(ln(M)/ln(2))
tolerance: 0.5

## How many bits do you need to give each of {M} items its own number?

::: misses
- [floor(ln(M)/ln(2))] One short: 2 to that power is less than {M}.
- [ceil(log10(M))] That's decimal digits. Bits only double each time.
- [any] Find the smallest N with $2^N \ge \given{M}$.
:::
:::

::: check
given: N = 10 | 12 | 14 | 16 | 24; V = 1 | 2.048 | 2.5 | 4.096
answer: V/2^N
tolerance: 0.5%

## An ADC with {N} bits has a range of 0 to {V} V. How big is one step?

::: misses
- [V/N] {N} bits make $2^{\given{N}}$ steps, not {N}.
- [any] Divide the range by $2^{\given{N}}$.
:::
:::

::: check
given: A = 16 | 20 | 24 | 28 | 32
answer: 2^A
tolerance: 0.5

## A processor has {A} address lines, and each address names one byte. How many bytes can it address?

::: misses
- [2*A] Each address line doubles the number of addresses.
- [2^A - 1] That's the highest address. Counting address 0, there's one more.
- [any] {A} lines name $2^{\given{A}}$ addresses.
:::
:::
:::
