AP · Computer Science Principles · Representing Data in Binary

Overflow, Round-Off and Analog Data

13 min readUpdated December 1, 2026FreeAP Computer Science Principles, Data

A fixed number of bits holds a limited range of values: n bits make 2ⁿ patterns, so a result that needs more bits causes an overflow error. Real numbers such as 0.1 are stored as close approximations, which causes round-off error, and analog data such as sound is stored by sampling it. In AP® Computer Science Principles, all three show that bits are a useful but limited abstraction.

Quick check

  1. What is 1011 in binary as a decimal number?Show answerHide answer
    1. The 1s sit in the 8, 2 and 1 places, and 8 + 2 + 1 = 11.
  2. Can a 4-bit counter that starts at 0 count all the way to 16?Show answerHide answer

    No. 4 bits give 2⁴ = 16 patterns, and one of them is 0, so the largest value is 15.

Know these already? Jump to the practice questions

What you'll be able to do

  • Decide whether a result overflows a fixed number of bits.
  • Explain why a real number such as 0.1 has round-off error.
  • Calculate how many bits a sampled recording needs.
  • Explain how one bit pattern can stand for a number, a character or a color.

Key terms

Overflow error

An error that occurs when a value needs more bits than the fixed number available to store it.

In plain English
The number is too big for its box.
Don't write
"Overflow means the computer ran out of memory."

Round-off error

The gap between a real number and the approximation a computer stores in a fixed number of bits.

In plain English
The stored value is very close to the true one, but not equal to it.
Don't write
"Round-off error only happens with huge numbers."

Sampling

Measuring an analog signal at regular intervals and storing each measurement in a fixed number of bits.

In plain English
Taking snapshots of a smooth signal.
Don't write
"Sampling copies the analog signal exactly."

Abstraction

Hiding details so something is simpler to use; the same bits can stand for numbers, text, colors or sound.

In plain English
Working with what the bits mean, not the bits themselves.
Don't write
"Abstraction means the data is deleted."

When does a fixed number of bits overflow?

Each extra bit doubles the number of patterns, so n bits make 2ⁿ patterns. For whole numbers starting at 0, the largest value is 2ⁿ − 1, because one pattern is used for 0. So 4 bits store 0 to 15, and 8 bits store 0 to 255.

Many languages store whole numbers in a fixed number of bits, so overflow happens when a result is larger than that ceiling. In a 4-bit system, 12 + 6 = 18, which is 10010 in binary: 5 bits, so it overflows.

What happens next depends on the language: some stop the program with an error, others keep only the bits that fit and store a wrong value. Python, by contrast, gives integers unlimited precision, so they can grow as large as memory allows.

Key idea

With n bits, the largest whole number you can store is 2ⁿ − 1, not 2ⁿ.

Check yourself

In an 8-bit system, does 200 + 56 overflow?
Show answerHide answer

Yes. 200 + 56 = 256, and the largest 8-bit value is 255, so 256 needs a ninth bit.

Why are some real numbers stored inexactly?

In binary, 0.1 never ends: it is 0.000110011001100… with 0011 repeating forever. A computer stores a real number in a fixed number of bits, so it must stop the pattern and round. The stored value is extremely close to 0.1, but not equal to it: that gap is round-off error.

Small gaps add up. In both JavaScript and Python, 0.1 + 0.2 gives 0.30000000000000004, and adding 0.1 ten times, starting from 0, gives 0.9999999999999999.

Fractions built from powers of 2 are exact: 0.5 is 0.1 in binary and 0.25 is 0.01.

Check yourself

Why can 0.75 be stored exactly in binary, but 0.1 cannot?
Show answerHide answer

0.75 is 1/2 + 1/4, which is 0.11 in binary and ends. 0.1 is not a finite sum of powers of 2, so its binary form repeats and must be rounded.

How does sampling turn analog data into bits?

Analog data changes smoothly, so it has a value at every instant: the pitch of a voice, for example. A computer stores only a finite list of values, so it samples the signal at regular intervals, storing each measurement in a fixed number of bits.

The result is an approximation. More samples per second, or more bits per sample, gets closer to the original but needs more bits:

bits = samples per second × seconds × bits per sample

Divide by 8 to get bytes.

Check yourself

A sensor takes 10 samples per second for 6 seconds, at 4 bits per sample. How many bits is that?
Show answerHide answer

240 bits. There are 10 × 6 = 60 samples, and 60 × 4 = 240.

How can the same bits mean different things?

A bit pattern means nothing until a program reads it. The byte 01001000 is 72 read as a whole number. Read as text in ASCII, a standard code for characters, 72 is the capital letter H.

A program that stores a color as three 8-bit amounts of red, green and blue can show 2⁸ × 2⁸ × 2⁸ = 2²⁴ = 16,777,216 colors. You work with a number, a letter or a color, and the bits stay hidden: that is abstraction.

Check yourself

What decimal number does the byte 00001111 represent when read as a whole number?
Show answerHide answer
  1. The four 1s are in the 8, 4, 2 and 1 places, and 8 + 4 + 2 + 1 = 15.

Worked example 1: Decide whether a score overflows

Question: A game stores scores as whole numbers in 6 bits. A player with 58 points earns 9 more. (a) What is the largest score 6 bits can store? (b) How many bits does the true new total need?

What tells you the method

A fixed bit size and a new result: find the limit, then compare.

  1. Step 1: Find the largest value.

    2⁶ − 1 = 63.

    Why

    6 bits make 64 patterns, and one of them is 0.

  2. Step 2: Compare the total.

    58 + 9 = 67 > 63, so the result overflows.

    Why

    A result above the limit cannot be stored correctly.

  3. Step 3: Count the bits the total needs.

    67 = 64 + 2 + 1, which is 1000011 in binary: 7 bits.

    Why

    The highest place used, 64 = 2⁶, is the seventh from the right.

Check it

64 ≤ 67 < 128 = 2⁷, so 67 needs exactly 7 bits.

Answer

(a) 63; (b) 7, so the total overflows.

Worked example 2: Size a sampled recording

Question: A hypothetical recorder takes 8,000 samples per second at 8 bits per sample. How many bytes does a 5-second clip need, before compression?

What tells you the method

A sample rate, a time and bits per sample: use the size rule.

  1. Step 1: Count the samples.

    8,000 × 5 = 40,000 samples.

    Why

    The recorder measures the sound 8,000 times each second.

  2. Step 2: Count the bits, then bytes.

    40,000 × 8 = 320,000 bits, and 320,000 ÷ 8 = 40,000 bytes.

    Why

    Each sample is 8 bits, and a byte is 8 bits.

Check it

Each 8-bit sample is 1 byte, so bytes equal samples: 40,000.

Answer

40000 bytes

Worked example 3: Trace a doubling loop

Question: This code doubles a reading until it no longer fits in 8 bits (0 to 255). What is displayed as a result of running it?

value ← 3
steps ← 0
REPEAT UNTIL(value > 255)
{
   value ← value * 2
   steps ← steps + 1
}
DISPLAY(steps)
DISPLAY(value)

What tells you the method

A loop with a stopping condition: trace it pass by pass.

  1. Step 1: Trace each pass.

    Passvaluesteps
    start30
    161
    2122
    3243
    4484
    5965
    61926
    73847
    Why

    REPEAT UNTIL checks the condition before each pass and stops once value > 255 is true.

  2. Step 2: Read off the display.

    The two DISPLAY calls show 7 and 384 on one line.

    Why

    Each DISPLAY shows its value followed by a space.

Check it

192 ≤ 255, so pass 7 runs; 384 > 255, so pass 8 does not.

Answer

7 384

Your turn

Question: A system stores whole numbers in 4 bits. A program adds 1011 and 0110, both in binary. What is the sum in decimal, and does it fit in 4 bits?

  1. Step 1: Convert the inputs.

    1011 is 8 + 2 + 1 = 11, and 0110 is 4 + 2 = 6.

  2. Step 2: Your go: add them and compare with the largest 4-bit value.

Show the full solutionHide the solution

Step 2. 11 + 6 = 17. The largest 4-bit value is 2⁴ − 1 = 15, and 17 > 15. In binary 17 is 10001, which needs 5 bits.

Answer: 17, which is more than 15, so the sum overflows.

Traps

Trap

With n bits you can store whole numbers up to 2ⁿ.

It seems right because n bits make 2ⁿ patterns. But one pattern is used for 0, so the largest value is 2ⁿ − 1: 8 bits store 0 to 255.

Does 256 fit in 8 bits?

No. The largest 8-bit value is 255; 256 is 100000000 in binary, which needs 9 bits.

Trap

Round-off error happens only with very large or very long numbers.

It seems right because rounding sounds like something only long decimals need. But 0.1 repeats forever in binary, so it is rounded too.

Which is stored exactly in binary: 0.1 or 0.25?

0.25. It is 1/4, which is 0.01 in binary; 0.1 repeats forever in binary.

What's examined

On the AP® Computer Science Principles exam, this topic belongs to Big Idea 2: Data. Expect multiple-choice questions on whether a value fits in a number of bits, why a result is approximate, what sampling does, and what a bit pattern means. All of it is Learn this: the exam reference sheet covers only the pseudocode.

Beyond this course: how a floating-point format splits its bits into sign, exponent and fraction.

Practice questions

  1. Recall Which statement describes round-off error?
  • A) A result needs more bits than the fixed number available.
  • B) A real number is stored as a close approximation in a fixed number of bits.
  • C) A continuous signal is measured at regular intervals.
  • D) Information is removed to make a file smaller.
Show answer

Answer: B. Round-off error is the gap between a real number and the approximation stored in a fixed number of bits.

Why the others are wrong:

  • A describes overflow error.
  • C describes sampling.
  • D describes lossy compression.
  1. Recall What is the largest whole number that can be stored in 10 bits?
Show answer

Answer: 1023

Marking points:

  • 10 bits make 2¹⁰ = 1,024 patterns.
  • One pattern is 0, so the largest value is 1,024 − 1 = 1,023.
  1. Apply A program stores whole numbers in 7 bits. Which calculation causes an overflow error?
  • A) 60 + 60
  • B) 127 + 0
  • C) 100 − 40
  • D) 90 + 40
Show answer

Answer: D. The largest 7-bit value is 2⁷ − 1 = 127, and 90 + 40 = 130 > 127.

Why the others are wrong:

  • A gives 120, which fits; it only looks too big if you take 2⁶ = 64, the leftmost place value, as the limit.
  • B gives 127, exactly the largest value, so it fits.
  • C gives 60, which fits.
  1. Apply A hypothetical sensor records a temperature 4 times per minute, at 12 bits per reading. How many bits do one hour of readings need?
Show answer

Answer: 2880

Marking points:

  • One hour is 60 minutes, so there are 4 × 60 = 240 samples.
  • 240 × 12 = 2,880 bits.
  1. Exam-style The byte 01000001 is stored in a file. Which statement is correct?
  • A) It could be the number 65 or the ASCII character A, depending on how a program reads it.
  • B) It is always 65, because bits store only numbers.
  • C) It is 130, reading the place values from the left.
  • D) It is only the character A, because one byte always holds one character.
Show answer

Answer: A. 01000001 is 64 + 1 = 65, and in ASCII 65 is the code for A; the program decides which meaning applies.

Why the others are wrong:

  • B ignores abstraction: the same bits can stand for text, colors or sound.
  • C reads the bits in reverse order, giving 10000010, which is 130.
  • D treats one interpretation as the only one.
  1. Exam-style A counter holds whole numbers up to 63. This code segment finds the first total that is too big. What is displayed as a result of running it?
total ← 0
count ← 0
REPEAT UNTIL(total > 63)
{
   total ← total + 10
   count ← count + 1
}
DISPLAY(count)
DISPLAY(total)
Show answer

Answer: 7 70

Marking points:

  • total goes 10, 20, 30, 40, 50, 60, 70 while count goes 1 to 7.
  • After pass 6, total is 60 ≤ 63, so pass 7 runs; after pass 7 it is 70 > 63, so the loop stops.
  1. Apply A song is recorded twice at 16 bits per sample: X takes 20,000 samples per second and Y takes 40,000. Which statement is true?
  • A) X needs twice as many bits as Y.
  • B) Both need the same bits, since bits per sample match.
  • C) Y needs twice as many bits and is usually a closer approximation.
  • D) Y is an exact copy of the original sound.
Show answer

Answer: C. Bits = samples per second × seconds × bits per sample, so doubling the sample rate doubles the bits and captures the sound more closely.

Why the others are wrong:

  • A has the relationship backwards.
  • B ignores the number of samples.
  • D forgets that any sampled recording is an approximation.

Key points

Recall, then reveal

Say the main points from memory first, then check them.

Show key pointsHide key points
  • n bits give 2ⁿ patterns and store whole numbers from 0 to 2ⁿ − 1.
  • A result above that limit overflows: the program stops or stores a wrong value, depending on the language.
  • Numbers like 0.1 repeat forever in binary, so they are rounded; sums of powers of 2 are exact.
  • Sampling gives an approximation of analog data, sized by samples per second × seconds × bits per sample.
  • Bits mean nothing until a program interprets them as a number, a character, a color or a sound.

Keep learning

Explore AP Computer Science Principles tutoring →

View the full Computer Science Principles syllabus →

More in this unit

Data

All Representing Data in Binary notes

Part of Novark's AP Computer Science Principles notes