Embercode
✕
How Computers WorkLesson 10 of 12

35 min read

Bits, bytes and units: KB vs KiB

Why this matters

A download page says a file is 50 MB. Your disk says the same file takes 47.7 MiB. A video site says your connection gives 100 Mbps. Your browser shows the download speed as 12.5 MB per second. These numbers all describe the same things, but they use different units, and a beginner reads them as contradictions. Some of them are not contradictions at all. Others are small errors that grow with the size of the file.

This lesson sets out the units and the rules for converting between them. You will count bits and bytes, learn the difference between KB and KiB, and use worked tables to estimate how big a file or a data set will be before you write any code. The lesson does not repeat how a computer stores a letter or a colour. The lessons on binary and on sound and video cover those.

Counting bits

A bit is one switch with two states, written 0 or 1. A single bit can hold two patterns: 0 or 1. Two bits can hold four patterns: 00, 01, 10 and 11. Each extra bit doubles the number of patterns, because every existing pattern can be followed by either a 0 or a 1.

This is the rule in one line. With n bits you get 2 to the power n patterns. "2 to the power n" means 2 multiplied by itself n times. Here is a program that lists the patterns for one, two and three bits.

Python · editable
1 bits: ['0', '1']
2 bits: ['00', '01', '10', '11']
3 bits: ['000', '001', '010', '011', '100', '101', '110', '111']
1 bits make 2 patterns
2 bits make 4 patterns
8 bits make 256 patterns
16 bits make 65536 patterns

A byte is a group of 8 bits. Eight bits make 256 patterns, which is enough for the numbers 0 to 255, or for one letter in a simple text code. Most storage and memory is counted in bytes. Speeds on networks are usually counted in bits. That split causes most of the confusion in this lesson.

BitsPatternsExample use
12yes or no
8256one byte, one letter in simple text
1665,536a whole number in many programs

The table shows that a count of bits grows quickly. Sixteen bits give 65,536 patterns. Thirty-two bits give over four billion. You will meet these numbers again when a program stores a number in a fixed number of bits.

Bits and bytes in one table

Use a small b for bits and a capital B for bytes. Writing Mb and MB for the same amount is the most common reason to get a download time wrong.

Written asMeansUsed for
bit, bone 0 or 1network speeds (Mbps: megabits per second)
byte, B8 bitsfile sizes and memory (MB, GB)

Convert bytes to bits by multiplying by 8. Convert bits to bytes by dividing by 8. A 50 MB file is 400 million bits. A 100 Mbps connection moves 100 million bits each second, so the file takes 4 seconds, ignoring overheads. Here is the calculation in a program.

Python · editable
50 MB at 100 Mbps: 4.0 seconds
100 Mbps in MB per second: 12.5

So a 100 Mbps link delivers 12.5 MB per second, not 100 MB per second. This is the first thing to check whenever a download seems too slow.

A harder example: a game over a slower link

A game is 2.5 GB. The download runs at 40 Mbps. Work through it in steps.

  1. 2.5 GB is 2.5 times 1,000,000,000 bytes, which is 2,500,000,000 bytes. Here GB means the decimal unit, explained in the next section.
  2. Multiply by 8 to get bits: 20,000,000,000 bits.
  3. Divide by the speed, 40,000,000 bits per second: 500 seconds.
  4. 500 seconds is 8 minutes and 20 seconds.

The program agrees.

Python · editable
seconds: 500.0
minutes: 8.333333333333334

Real downloads are slower than this, because the link is shared and the protocol adds its own bytes. The calculation gives the best case, which is what you need for a first estimate.

KB and KiB: two families of prefixes

A prefix tells you how many times to multiply. "Kilo" means a thousand, so a kilobyte in the everyday decimal sense is 1,000 bytes. Computer memory is built from powers of two, so a programmer often wanted 1,024 instead, which is 2 to the power 10. For a while both meanings were used under the same name. In 1998 the International Electrotechnical Commission (IEC) defined separate names for the binary sizes. The binary names have an i in them.

  • Decimal (SI) names: KB, MB, GB, TB. Each step is 1,000 times the one before.
  • Binary (IEC) names: KiB, MiB, GiB, TiB. Each step is 1,024 times the one before.

The i tells you the binary meaning. A size written as 4 MiB is 4 times 1,048,576 bytes. A size written as 4 MB is 4 times 1,000,000 bytes. The difference looks small at first, and it grows with each step.

PrefixDecimal bytesBinary bytesBinary is larger by
K (kilo, Ki)1,0001,0242.4%
M (mega, Mi)1,000,0001,048,5764.9%
G (giga, Gi)1,000,000,0001,073,741,8247.4%
T (tera, Ti)1,000,000,000,0001,099,511,627,77610.0%

Here is the table produced by a program, so you can check each number.

Python · editable
KB =            1,000 bytes   KiB =            1,024 bytes   gap 2.4%
MB =        1,000,000 bytes   MiB =        1,048,576 bytes   gap 4.9%
GB =    1,000,000,000 bytes   GiB =    1,073,741,824 bytes   gap 7.4%
TB = 1,000,000,000,000 bytes   TiB = 1,099,511,627,776 bytes   gap 10.0%

The rule for the gap is simple. The bigger the prefix, the bigger the gap. At the terabyte level, the same drive is 10 percent smaller when you count in binary units. This is the reason a drive labelled 1 TB looks smaller when the computer reports it.

Who uses which

Network speeds and drive labels almost always use decimal names. Operating systems and many programmers count memory and file sizes in binary, but many programs still print the names KB, MB and GB for binary sizes. The same file can therefore have two correct sizes, depending on which tool you use. Look for the i before you trust a number when the difference matters.

The drive that looks smaller

A drive maker sells a 500 GB drive. The label means 500 decimal gigabytes, which is 500,000,000,000 bytes. A system that counts in binary divides that by 1,024 three times and reports the result as GiB. The answer is about 465.7 GiB. Nothing is missing. The same bytes are being counted with a smaller unit, and the number is smaller.

The table below repeats the calculation for four common drive sizes.

Label on the boxBytesShown in GiB
256 GB256,000,000,000238.4
500 GB500,000,000,000465.7
1 TB1,000,000,000,000931.3
2 TB2,000,000,000,0001,862.6

A program reproduces the table.

Python · editable
label 256 GB = 256,000,000,000 bytes = 238.4 GiB
label 500 GB = 500,000,000,000 bytes = 465.7 GiB
label 1000 GB = 1,000,000,000,000 bytes = 931.3 GiB
label 2000 GB = 2,000,000,000,000 bytes = 1,862.6 GiB

The 1 TB row is the one people notice most. The box says 1 TB and the computer says 931 GiB. Both are correct. Each one uses a different unit.

Converting a size by hand

Two divisions cover almost every case.

  • To get KB from bytes, divide by 1,000. To get KiB, divide by 1,024.
  • To get MB from bytes, divide by 1,000,000. To get MiB, divide by 1,048,576.

The table below shows the same sizes in both units.

BytesShown in KBShown in KiB
9991.0 KB0.98 KiB
1,5001.5 KB1.46 KiB
1,048,5761,048.6 KB1,024.0 KiB
5,000,0005,000.0 KB4,882.8 KiB

The first row looks wrong at first. 999 bytes is 0.999 KB, which rounds to "1.0 KB" at one decimal place. Keep the exact byte count when you need precision.

The last row shows the gap at work. A file of 5 million bytes is 5,000 KB but only about 4,883 KiB. If you read the KB figure as KiB, you overstate the size by 2.4 percent. If you read a KiB figure as KB, you understate it by about 2.3 percent.

Measuring a real file

A program can tell you the exact size of a file in bytes. The operating system stores that count for every file. The function os.path.getsize returns it. The example below writes a file, measures it, and prints both units.

Python · editable
   1,000,000 bytes       1,000.0 KB         976.6 KiB

Notice the file has 999,999 letters and one newline. Each letter is one byte in this encoding, so the file is exactly one million bytes. The file manager of your computer might show it as 1 MB or as 976.6 KiB, depending on the tool.

A file manager can also show "Size on disk" next to the size. That number is larger for small files because the disk gives out space in blocks. The lesson on storage and files explains the blocks. For this lesson, the byte count is what you use for estimates.

Estimating a file before you write it

Estimating is simple multiplication. Work out the size of one item, then multiply by how many items you will store. Check the result in both units if the number matters.

Worked example 1: a log of one million records

Each record in a log holds a timestamp, a user number and a short message, and you estimate 100 bytes per record. The log has one million records.

  • Bytes: 1,000,000 times 100 is 100,000,000 bytes.
  • Decimal: 100,000,000 divided by 1,000,000 is 100 MB.
  • Binary: 100,000,000 divided by 1,048,576 is about 95.4 MiB.

A log written in plain text takes about 95 MiB on a system that counts in binary, and 100 MB on a system that counts in decimal. Both are right. Pick one unit for your report and write the unit next to the number.

Worked example 2: two million messages

A chat service stores two million messages, each about 250 bytes.

  • Bytes: 2,000,000 times 250 is 500,000,000 bytes.
  • Decimal: 500 MB.
  • Binary: about 476.8 MiB.

The answer is half a gigabyte in decimal units. If the database adds an index, a second copy of part of the data, add a margin for it; 20 to 50 percent is a rough guess, so measure a test database when it matters. An estimate that leaves out the index runs low.

Worked example 3: a spreadsheet exported as CSV

A CSV file stores one row per line, with commas between fields. The storage and files lesson introduces CSV. Here is the size estimate. Take one sample line and count its characters. Each character is one byte if the file uses plain letters, digits and punctuation.

Python · editable
characters in one sample row: 44
estimate for 200,000 rows: 8800000 bytes
real file: 8737805 bytes

The estimate is about 0.7 percent too large. The sample row has a units value of 45 and a price of 24.50, while the real rows vary, and some of them have shorter numbers. A one-line sample is good enough for a first estimate. If you need the exact size, measure the file.

Letters with accents, such as é, and symbols such as € take more than one byte in the common UTF-8 encoding. A text estimate that uses one byte per character is a lower bound for that kind of text.

Common mistakes

Mixing up Mbps and MB per second

A link rated at 100 Mbps delivers about 12.5 MB per second, because there are 8 bits in a byte. If you read 100 Mbps as 100 MB per second, your estimate is eight times too optimistic. A 1 GB file that should take about 80 seconds will look like it takes 10.

Reading KiB as KB, or the reverse

The gap is small for kilobytes and grows to 10 percent for terabytes. A program that reports 2.8 GB might mean 2.8 GiB. If you compare that number with the size on a box that says GB, check the unit. The mistake is easy to miss, because both numbers look right.

Converting a size stored as text

A program that reads a size from a text file gets a string, such as 1.5 KB, not a number. Converting the string with int fails. The message is a clear one, but beginners often do not read it carefully.

Python · editable
ValueError: invalid literal for int() with base 10: '1.5 KB'

The message says the text 1.5 KB is not a whole number in base 10. The unit is the problem. Store the size as a plain number of bytes, or split the text into the number and the unit before you convert it. Also note that int only accepts whole numbers, so "1.5" fails too. Use float for numbers with a decimal point.

Forgetting the unit in a report

A number without a unit is not a size. "The backup is 4.2" means nothing. Write "4.2 GiB" or "4.2 GB". Put the unit in the column header of a table as well.

Assuming the size on disk equals the size of the data

Small files take a whole block on disk, so a folder of tiny files can use far more space than the sum of their sizes. The storage lesson covers blocks. When you estimate space for many small files, multiply the count by the block size, not by the average byte count.

Practice

Exercise 1

How many bits are in 12 bytes?

Show the answer

96 bits. Each byte has 8 bits, so 12 times 8 is 96.

Exercise 2

How many different patterns can 5 bits hold?

Show the answer

32 patterns. The count is 2 to the power 5, which is 2 times 2 times 2 times 2 times 2.

Exercise 3

A file is 300 MB in decimal units. It downloads over a link of 30 Mbps. About how many seconds does it take, ignoring overheads?

Show the answer

80 seconds. 300 MB is 300,000,000 bytes, which is 2,400,000,000 bits. Divide by 30,000,000 bits per second to get 80 seconds.

Exercise 4

A file is exactly 2,000,000 bytes. A tool that counts in binary units shows its size in KiB, rounded to one decimal place. What does it show?

Show the answer

1,953.1 KiB. Divide 2,000,000 by 1,024 to get 1,953.125, which rounds to 1,953.1.

Exercise 5

A drive box says 1 TB. How many GiB does the computer report for it, to one decimal place?

Show the answer

931.3 GiB. The drive has 1,000,000,000,000 bytes. Divide by 1,024 to the power 3 (1,073,741,824) to get about 931.3.

Summary

  • A bit is one 0 or 1. With n bits you can make 2 to the power n patterns, and a byte is 8 bits.
  • Network speeds use bits per second (Mbps). File sizes use bytes (MB). Divide an Mbps figure by 8 to get MB per second.
  • KB, MB, GB and TB use 1,000 as the step. KiB, MiB, GiB and TiB use 1,024. The gap grows from 2.4 percent at kilo to 10 percent at tera.
  • A drive label uses decimal units, so a 1 TB drive reports about 931 GiB on a binary system.
  • To estimate a data set, multiply the size of one item by the number of items, then check the unit.
  • Measure a real file with the operating system when the number matters, and keep the unit with every number you write down.

Stuck? Ask the tutor

Ask about anything in this lesson. Answers are short and use simple words.

Sign in to ask the tutor.

← Sound and video as samples