Skip to content
Ameen Abozaid
PublishedBEG-001-L03

Bits, bytes, and binary numbers

  • Lesson 3 of 6
  • 14 min read
  • Beginner
On This Lesson 11 topics

Lesson Overview

In the last lesson you saw that RAM and storage hold data. But what is that data made of? A photo, a song, and a message all look very different to you. Inside a computer they are all stored the same way: as long rows of ones and zeros.

In this lesson you will learn what a bit and a byte are, how to read and write numbers in binary, how to convert small numbers by hand, and how to work out how many different values a given number of bits can hold. You will also learn why "3 MB" can mean two slightly different numbers of bytes.

Why this matters: file sizes, memory sizes, network speeds, number limits in programs, and even why some bugs appear all come back to bits and bytes. You do not need to be good at maths. If you can add and compare small numbers, you can do everything in this lesson.

This lesson builds on the previous two: data lives in RAM and in storage, and the CPU works on it.

Learning Objectives

By the end of this lesson you will be able to:

  • Define a bit and a byte, and state how many bits are in a byte.
  • Read the place values of a binary number (1, 2, 4, 8, 16, 32, 64, 128 and so on).
  • Convert small binary numbers to decimal, and small decimal numbers to binary, by hand.
  • Calculate how many different values n bits can hold, and the largest number they can store.
  • Explain, at a high level, how text and colours can be stored as numbers.
  • Explain why a megabyte can mean one million bytes or 1,048,576 bytes, and which names go with which.

Written Explanation

Why only two symbols?

Electronic circuits are very good at telling two states apart: on or off, high voltage or low voltage. Telling ten states apart (for decimal digits 0 to 9) reliably and cheaply is much harder. So computers use just two symbols, written as 0 and 1, and build everything else out of long rows of them.

A single 0 or 1 is called a bit, short for "binary digit". A bit is the smallest unit of data: a yes/no answer.

One bit can hold only two values, which is not much. So bits are grouped. A group of 8 bits is called a byte. A byte is the unit in which memory and storage sizes are usually counted. When you hear "a 16 GB phone", the "B" stands for bytes.

Decimal: the system you already know

You already use a place-value system: decimal, or base 10. In the number 4,305, each digit has a place worth ten times the place to its right:

Placethousandshundredstensones
Value1000100101
Digit4305

So 4,305 means 4 x 1000 + 3 x 100 + 0 x 10 + 5 x 1. Ten symbols (0 to 9) are available in each place. When a place reaches 10, you carry into the next place.

Binary: the same idea with two symbols

Binary (base 2) works in exactly the same way, with two differences: each place is worth two times the place to its right, and only the digits 0 and 1 exist.

Reading from the right, the place values are:

Place1286432168421
In powers of two2^72^62^52^42^32^22^12^0

Memorise this row. It continues, always doubling: 256, 512, 1024, and so on.

Binary to decimal

To convert a binary number to decimal, add up the place values that have a 1.

Example: 101010

Place32168421
Digit101010

The ones are in the 32, 8, and 2 places, so the value is 32 + 8 + 2 = 42.

Reading the binary number 00101010A row of eight place values: 128, 64, 32, 16, 8, 4, 2 and 1 (the powers of two from 2 to the 7th down to 2 to the 0). Under them is the binary number 0 0 1 0 1 0 1 0. The ones are under 32, 8 and 2, and 32 plus 8 plus 2 equals 42.PlaceDigit1282⁷0642⁶0322⁵1162⁴082³142²022¹112⁰0Add the place values that hold a 1:32 + 8 + 2 = 42
Figure. Each place is worth twice the place to its right. The digits 1 sit in the 32, 8 and 2 places, so 00101010 is 32 + 8 + 2 = 42.
Text version of this diagram

A row of eight place values: 128, 64, 32, 16, 8, 4, 2 and 1 (the powers of two from 2 to the 7th down to 2 to the 0). Under them is the binary number 0 0 1 0 1 0 1 0. The ones are under 32, 8 and 2, and 32 plus 8 plus 2 equals 42.

Leading zeros do not change the value, just as 007 is still 7: 00101010 is also 42. Programmers often write binary in groups of 8 digits (a byte) so they line up.

Decimal to binary

To convert a decimal number to binary, go through the place values from the largest that fits down to 1. At each place:

  • If the place value is less than or equal to what is left, write 1 and subtract it.
  • Otherwise write 0.

Example: convert 86.

PlaceFits in what is left?DigitLeft after
128No (128 > 86)086
64Yes122
32No (32 > 22)022
16Yes16
8No (8 > 6)06
4Yes12
2Yes10
1No (nothing left)00

Reading the digits top to bottom gives 01010110. Check it by converting back: 64 + 16 + 4 + 2 = 86. Always do this check.

Counting in binary

Counting works like decimal counting, except you carry when a place reaches 2, not 10:

Decimal012345678
Binary0110111001011101111000

Notice that 1000 (eight) is a 1 followed by three 0s, just as 1000 in decimal is a 1 followed by three 0s. Each time you add a place, the number of values doubles.

How many values can n bits hold?

Each bit has 2 possible values, and every extra bit doubles the number of patterns:

BitsDifferent patternsValues it can represent (counting from 0)
120 to 1
240 to 3
380 to 7
4160 to 15
82560 to 255
1665,5360 to 65,535

The rule: n bits can hold 2^n different patterns, so counting from 0 the largest whole number is 2^n - 1. A byte (8 bits) gives 256 patterns, so it can hold the numbers 0 to 255. This is why 255 appears so often in computing.

Everything is numbers

If a byte can hold a number, it can hold anything we agree to treat as a number.

Text. Each character is given a number by an agreed table. In the common ASCII and Unicode tables, H is 72 and i is 105, so the word Hi is stored as those two numbers: 01001000 01101001. Different tables (and different sizes for characters outside English) exist, and the details come later. The idea to keep is: text is numbers plus an agreed table.

Colours. A screen colour is usually described by how much red, green, and blue to mix, each from 0 to 255, so one byte each. Three bytes per colour gives 256 x 256 x 256 = 16,777,216 possible colours.

Photos. A photo is a grid of coloured dots (pixels). A photo is therefore a very long list of numbers. Real photo files are normally compressed, which means rewritten in a shorter form, so they take less space than the raw list would.

The computer does not know whether a row of bits is a number, a letter, or a colour. The program decides how to interpret it. This will matter when you meet programming languages and their types.

Measuring size: bytes, kilobytes, and the 1000 versus 1024 puzzle

Bigger quantities use prefixes:

NameSymbolMeaning
kilobytekB1,000 bytes
megabyteMB1,000,000 bytes
gigabyteGB1,000,000,000 bytes
terabyteTB1,000,000,000,000 bytes

These are the standard SI prefixes: "kilo" means 1,000, "mega" 1,000,000, and so on.

But because computers count in powers of two, people also used "kilo" for 1,024 (which is 2^10), "mega" for 1,048,576 (which is 2^20), and so on. To remove the ambiguity, an international standard introduced separate names: kibibyte (KiB) = 1,024 bytes, mebibyte (MiB) = 1,048,576 bytes, gibibyte (GiB), and so on. NIST (the US National Institute of Standards and Technology) documents these binary prefixes.

In practice:

  • Storage makers usually advertise capacity with the 1,000-based units.
  • Some software reports sizes in 1,024-based units but may still label them KB, MB, or GB.
  • That is why a drive sold as "500 GB" can appear as a smaller number in some programs. Nothing is missing. The two sides are counting with different units.

So "3 MB" means about 3,000,000 bytes in SI units, and 3 MiB means 3,145,728 bytes. When precision matters, check which convention is used.

Finally, do not confuse bits and bytes. Network speeds are usually quoted in bits per second (written with a lowercase b, as in Mbps), while file sizes use bytes (capital B). There are 8 bits in a byte, so a connection of 80 Mbps moves at most about 10 megabytes per second, before overheads.

Examples

Example 1: binary to decimal (simple)

Convert 1101 to decimal.

Place values from the right: 1, 2, 4, 8. The ones are in the 8, 4, and 1 places.

8 + 4 + 1 = 13.

Example 2: decimal to binary (practical)

Convert 37 to binary using the method.

  • 32 fits, left 5. Digit 1.
  • 16 does not fit. Digit 0.
  • 8 does not fit. Digit 0.
  • 4 fits, left 1. Digit 1.
  • 2 does not fit. Digit 0.
  • 1 fits, left 0. Digit 1.

The digits are 100101. Check: 32 + 4 + 1 = 37.

Example 3: how big can this number be? (realistic)

A program stores a player's age in one byte. What is the largest age it can store, and what happens at 300?

One byte has 8 bits, so it holds 0 to 255. Ages up to 255 fit. A value of 300 does not fit in one byte: the program needs a larger space (more bits) or it will store the wrong value. Choosing a space big enough for the largest value is something you will do in real programs.

Backend Insight

Bits and bytes appear everywhere in backend work:

  • Number limits. A programming language stores whole numbers in a fixed number of bits. A 32-bit whole number tops out at 2,147,483,647 if it can be negative, which a very busy system can pass. IDs and counters that outgrow their space are a classic cause of production bugs.
  • Sizes and costs. Cloud storage, memory, and data transfer are bought in bytes. Knowing roughly how many bytes a record takes lets you estimate how much a million of them will cost.
  • Network. Data sent between servers is sent as bytes. Speeds are in bits per second, sizes in bytes, and mixing them up leads to wrong estimates by a factor of 8.
  • Text. A character may take more than one byte, so "length in characters" and "size in bytes" can differ. This affects limits on fields and messages.

You will meet each of these again. For now, remember: everything the backend handles is bytes, and the program decides what they mean.

Common Mistakes

Mistake 1: Reading binary as a decimal number

  • Incorrect: "101 is one hundred and one."
  • Correct: "101 in binary is 4 + 1 = 5."

Always say which system you are in. When it might be unclear, write the base: 101 in binary is 5.

Mistake 2: Writing the digits in the wrong order

The right-most digit is worth 1 (the smallest place), and the places grow to the left. Reading from the wrong end gives a different number.

Mistake 3: Thinking n bits hold n values

  • Incorrect: "8 bits hold 8 values."
  • Correct: "8 bits hold 2^8 = 256 patterns, so 0 to 255."

Mistake 4: Forgetting that counting starts at 0

  • Incorrect: "A byte can store the number 256."
  • Correct: "A byte has 256 patterns, but they start at 0, so the largest number is 255."

Mistake 5: Mixing up bits and bytes, or 1,000 and 1,024

A lowercase b means bits and a capital B means bytes. Eight bits make one byte. If the unit matters, check whether 1,000-based or 1,024-based units are being used.

Exercises

Try each exercise yourself before opening the solutions. Use the place-value table.

Level A: Foundation

A1. How many bits are in a byte? What is the smallest unit of data called?

Show solution A1

A1. A byte has 8 bits. The smallest unit of data is a bit.

A2. Convert to decimal: 1001, 10110, 111111.

Show solution A2

A2. 1001 = 8 + 1 = 9. 10110 = 16 + 4 + 2 = 22. 111111 = 32 + 16 + 8 + 4 + 2 + 1 = 63.

A3. Convert to binary: 9, 25, 50.

Show solution A3

A3. 9 = 8 + 1 = 1001. 25 = 16 + 8 + 1 = 11001. 50 = 32 + 16 + 2 = 110010.

Level B: Application

B1. Write the numbers 0 to 8 in binary without looking at the table, then check against the table in the lesson.

Show solution B1

B1. 0 = 0, 1 = 1, 2 = 10, 3 = 11, 4 = 100, 5 = 101, 6 = 110, 7 = 111, 8 = 1000.

B2. How many different patterns can 4 bits hold? 5 bits? 12 bits? What is the largest whole number that 5 bits can hold?

Show solution B2

B2. 4 bits: 2^4 = 16 patterns. 5 bits: 2^5 = 32. 12 bits: 2^12 = 4,096. The largest number in 5 bits is 2^5 - 1 = 31.

B3. Add the binary numbers 101 and 11 by converting to decimal, adding, and converting back.

Show solution B3

B3. 101 = 5 and 11 = 3. 5 + 3 = 8, which is 1000 in binary.

B4. The word Hi is stored as 72 and 105. Write 72 in binary as a full 8-digit byte.

Show solution B4

B4. 72 = 64 + 8, so the digits are 1 in the 64 place, 1 in the 8 place, and 0 elsewhere: 01001000.

Level C: Challenge

C1. A drive is sold as "250 GB", meaning 250,000,000,000 bytes. A program reports its size in 1,073,741,824-byte units (GiB). Roughly what size does it show?

Show solution C1

C1. 250,000,000,000 / 1,073,741,824 is about 232.8, so the program shows roughly 233 GiB (some programs label this "GB"). Nothing is missing. The units are different.

C2. You have a connection of 100 Mbps. At most, roughly how many megabytes per second can it carry? Why is your real download speed likely lower?

Show solution C2

C2. 100 Mbps = 100 million bits per second. Divide by 8 bits per byte: 12.5 megabytes per second at most. Real speeds are lower because of protocol overhead, shared connections, the speed of the server, and other factors.

C3. A colour uses one byte each for red, green, and blue. How many bytes does one pixel take? How many bytes would a raw (uncompressed) photo of 2,000 x 1,000 pixels take? Why are real photo files usually smaller?

Show solution C3

C3. One pixel takes 3 bytes. A 2,000 x 1,000 photo has 2,000,000 pixels, so 2,000,000 x 3 = 6,000,000 bytes (about 6 MB) uncompressed. Real photo files are usually compressed, which stores the same picture in fewer bytes.

Mini Challenge

Create your own "binary cheat sheet" on one page:

  1. The place values from 1 up to 1024.
  2. The binary form of your age, the day of your birth, and the number of the month you were born in.
  3. How many patterns 10 bits can hold, with the rule you used.
  4. A one-sentence reminder of the difference between Mb and MB, and between kB and KiB.

Check your work: convert each of your three binary numbers back to decimal and confirm you get the original number.

Summary & Next Lesson

Key takeaways

  • A bit is one 0 or 1. A byte is 8 bits.
  • Binary is a place-value system where each place is worth twice the one to its right.
  • Convert binary to decimal by adding the place values that have a 1. Convert decimal to binary by working down the place values and subtracting.
  • n bits give 2^n patterns, from 0 to 2^n - 1. A byte holds 0 to 255.
  • Text, colours, and photos are all stored as numbers plus an agreed way to interpret them.
  • kB, MB, GB use 1,000-based steps. KiB, MiB, GiB use 1,024-based steps. Lowercase b is bits, capital B is bytes.

Next lesson

You know what the parts are and what the data is made of. The next lesson, "The operating system's job", explains the software that manages all of this so many programs can share one CPU, one RAM, and one storage device.

Self-check

  1. What is a byte?
  2. What is 1010 in decimal?
  3. What is 20 in binary?
  4. How many values can 10 bits hold?
  5. Is 3 MB exactly 3 x 1,048,576 bytes?
Show answers
  1. A group of 8 bits.
  2. 8 + 2 = 10.
  3. 20 = 16 + 4 = 10100.
  4. 2^10 = 1,024 patterns.
  5. Not necessarily. With SI units, 3 MB is 3,000,000 bytes. 3 MiB is 3 x 1,048,576 = 3,145,728 bytes. Check which convention is used.

References

  • Kansas State University, CC 110 textbook, Binary Numbers. It explains binary place values and conversion in both directions, with the worked examples 00101010 = 42 and 86 = 01010110 used in this lesson. It does not define bits and bytes, so those definitions are standard terminology.
  • NIST, Prefixes for binary multiples. It distinguishes SI prefixes (kilo = 1,000) from the binary prefixes kibi, mebi, and gibi (1,024-based).

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.