Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 55 ยท Data Representation

Bits and Bytes

Learn why computers use two states, what bits, bytes and words are, how KB differs from KiB, and how to estimate how much data fits on a disk or in memory.

Inside a computer there are no letters, no pictures and no songs. There are only huge numbers of tiny switches that are either off or on. Everything you see, from a message to a movie, is a pattern of those two states. This lesson explains why computers settled on two states, introduces the bit and the byte, clears up the confusing size units (why your 1 TB drive shows less than 1 TB), and gives you a feel for how much data fits where.

If the idea of a processor and memory is new, read What Is a Computer? first. The next lesson, Binary, Octal and Hexadecimal, shows how to read and write these patterns as numbers.

Why only two states?

A transistor is a switch controlled by electricity. It is easy to build a switch that is clearly off (low voltage) or clearly on (high voltage). Telling apart ten different voltage levels, to represent the digits 0 to 9, would be much harder, because heat, noise and ageing blur the levels. Two states are easy to tell apart even in a messy world, so billions of such switches can work reliably.

A single two-state value is a bit (short for binary digit). We write it as 0 or 1. One bit answers one yes/no question: is the light on? is this pixel black? A bit alone carries little, but patterns multiply quickly:

Number of bits Different patterns Example use
1 2 on / off
2 4 00, 01, 10, 11
3 8 eight colors in a tiny palette
8 256 one byte
16 65,536 16-bit audio sample range
32 4,294,967,296 about 4.3 billion
64 18,446,744,073,709,551,616 about 1.8 x 10^19

Every extra bit doubles the number of patterns. With n bits you can make 2^n different patterns. This is the single most useful fact about bits.

Bytes

Eight bits form a byte. A byte has 256 patterns, enough to hold one character of basic English text, a number from 0 to 255, or the brightness of one color channel in a pixel. The byte is the smallest unit that computers normally address in memory: each byte has its own location number, as you will see in Memory: RAM, Cache and the Memory Hierarchy.

Smaller groups have names too: a group of 4 bits is a nibble (one hexadecimal digit, see the next lesson). Larger groups are called words. A word is the natural chunk size a particular processor handles in one step. On a 64-bit processor a word is usually 64 bits (8 bytes). Word size also limits how big a number or memory address the processor can handle at once.

flowchart LR
  B["1 bit<br/>0 or 1"] --> N["4 bits<br/>nibble"]
  N --> By["8 bits<br/>1 byte"]
  By --> W["64 bits<br/>a typical word<br/>on a 64-bit CPU"]

Bits are grouped into nibbles, bytes and words.

Size units: KB, KiB and the 1000 versus 1024 puzzle

Data sizes use prefixes borrowed from science, and there are two families.

  • Decimal (SI) prefixes step by 1,000: kilobyte (KB) = 1,000 bytes, megabyte (MB) = 1,000,000 bytes, gigabyte (GB) = 10^9, terabyte (TB) = 10^12.
  • Binary (IEC) prefixes step by 1,024 (2^10): kibibyte (KiB) = 1,024 bytes, mebibyte (MiB) = 1,048,576 bytes, gibibyte (GiB) = 2^30, tebibyte (TiB) = 2^40.

Why 1,024? Memory is naturally built in powers of two, and 2^10 = 1,024 happens to be close to 1,000. Early on people called both "kilobyte", which caused years of confusion. The IEC prefixes (KiB, MiB, GiB) were introduced to be precise.

Now the famous puzzle. A drive maker sells a "1 TB" disk meaning 1,000,000,000,000 bytes. Some operating systems divide by 1,024 three times and report that as about 931 "GB", although they mean GiB. Nothing is missing; the two sides count with different units. Which units your system shows depends on the operating system.

JS Size calculator: decimal vs binary units

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Read the first line: the same number of bytes is "1.00 TB" in decimal units and "931.32 GiB" in binary units. The drive is exactly as large as advertised.

Use b for bits and B for bytes. Network speeds are usually advertised in bits per second (a 100 Mbps connection) while file sizes are in bytes. Divide by 8 to compare: 100 Mbps is at most 12.5 MB per second.

How much fits where?

Rough sizes help you think about storage. These are typical, not exact:

Item Approximate size
A short text message a few hundred bytes
A page of plain text 2 to 4 KB
A web page (HTML only) tens of KB
A phone photo (JPEG) 2 to 5 MB
A 4-minute song (MP3) 3 to 5 MB
An hour of HD video 1 to 4 GB, depending on compression
A big video game tens to over 100 GB

The estimates are why the same disk may hold either hundreds of thousands of photos or only a few dozen films. Compression and File Formats explains why file sizes vary so much.

Working with bits in code

Programming languages let you look at bits directly. In JavaScript, toString(2) writes a number in binary, and the operators & (AND), | (OR), ^ (XOR) and << (shift) work on the bits of 32-bit integers. This TryIt is a stand-in for a memory cell: it holds one byte as an array of eight switches.

JS Toggle the bits of a byte

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last line shows a handy rule: to give 1,000 different items their own code you need 10 bits, because 2^10 = 1,024 is the smallest power of two that is at least 1,000.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many different patterns can 10 bits represent?

    Each added bit doubles the patterns, so n bits give 2^n. With 10 bits that is 2^10 = 1,024.
  2. 2. Why do computers use two states instead of ten?

    Telling an off switch from an on switch is robust; telling many voltage levels apart is error-prone.
  3. 3. A disk sold as 1 TB (10^12 bytes) is shown as about 931 GiB. What explains the difference?

    10^12 bytes is exactly 1 TB in decimal units and about 931.32 GiB in binary units. The size is the same, only the unit differs.
  4. 4. A connection is advertised as 80 Mbps. What is the most data it can move per second in megabytes?

    Mbps means megabits per second. Divide by 8 bits per byte: 80 / 8 = 10 MB per second.

Summary

  • A bit is a two-state value; a byte is 8 bits (256 patterns); a word is the natural chunk of a processor.
  • n bits give 2^n patterns, so every extra bit doubles the possibilities.
  • KB, MB, GB use steps of 1,000; KiB, MiB, GiB use steps of 1,024.
  • Disks are advertised in decimal units, which is why systems may show smaller numbers.
  • Network speeds use bits per second; file sizes use bytes.
  • Rough sizes (text, photo, song, video) help you estimate storage needs.

Continue with Binary, Octal and Hexadecimal.