Back to homeAP® Computer Science Principles · AP Computer Science Principles · Change subject
Start for free
Notes

Exercises: Binary Numbers and Overflow

Practice★☆☆
Convert the binary number $1011$ to decimal. Then convert the 8-bit binary number $00110101$ to decimal.

Convert the binary number $1011$ to decimal. Then convert the 8-bit binary number $00110101$ to decimal.

Solve
Practice★☆☆
Convert the decimal number $25$ to an 8-bit binary number. Show the place values ($128, 64, 32, \ldots$) that you used.

Convert the decimal number $25$ to an 8-bit binary number. Show the place values ($128, 64, 32, \ldots$) that you used.

Solve
Practice★☆☆
A bit is a single 0 or 1, and a byte is 8 bits. a) How many different values can be represented with 6 bits? b) How many bytes are needed to store exactly 40 bits?

A bit is a single 0 or 1, and a byte is 8 bits.

  1. a) How many different values can be represented with 6 bits?
  1. b) How many bytes are needed to store exactly 40 bits?
Solve
Practice★★☆
A simple calculator stores unsigned whole numbers using only 4 bits, so the largest value it can hold is $15$. Aaliyah asks it to compute $9 + 8$. a) What is the true…

A simple calculator stores unsigned whole numbers using only 4 bits, so the largest value it can hold is $15$. Aaliyah asks it to compute $9 + 8$.

  1. a) What is the true sum, written in decimal and in binary?
  1. b) What happens inside the calculator, and what value does it end up storing? (Hint: think of what happens to the extra bit.)
  1. c) What is the name of this kind of error?
Solve
Practice★★☆
A school with 300 clubs wants to give each club a unique ID number stored as a binary number with the same number of bits for every club. a) What is the minimum number…

A school with 300 clubs wants to give each club a unique ID number stored as a binary number with the same number of bits for every club.

  1. a) What is the minimum number of bits needed?
  1. b) If the school adds one more bit to the ID, how many total clubs can it identify?
  1. c) Explain in one or two sentences why adding one bit doubles the number of values.
Solve
Practice★★☆
In many programming languages, the expression 0.1 + 0.2 = 0.3 evaluates to false, even though it is true in math class. a) Explain why this happens. Use the idea that…

In many programming languages, the expression 0.1 + 0.2 = 0.3 evaluates to false, even though it is true in math class.

  1. a) Explain why this happens. Use the idea that computers store numbers using a limited number of bits.
  1. b) What is the name for this kind of error?
  1. c) Describe a safer way for a program to check whether two decimal results are "equal."
Solve
Practice★★★
Marcus is writing a coin-collecting game. The score is stored in an 8-bit unsigned counter, and each coin is worth 25 points. The game allows up to 10,000 coins in a…

Marcus is writing a coin-collecting game. The score is stored in an 8-bit unsigned counter, and each coin is worth 25 points. The game allows up to 10,000 coins in a level.

  1. a) After how many coins does the counter first overflow? What score does the screen show after 11 coins? Assume that overflow wraps around to the remainder when divided by $256$.
  1. b) Explain why a player would see this as a bug.
  1. c) What is the minimum number of bits Marcus needs so that the score never overflows, even if a player collects all 10,000 coins? Show the numbers that support your answer.
Solve
Practice★★★
The 8-bit pattern $01000001$ could mean the number $65$, the letter "A", or the brightness of one pixel in a photo. a) Explain how a computer "knows" which meaning to…

The 8-bit pattern $01000001$ could mean the number $65$, the letter "A", or the brightness of one pixel in a photo.

  1. a) Explain how a computer "knows" which meaning to use for the same pattern.
  1. b) A microphone signal is sampled 1,000 times per second for 3 seconds. Each sample is stored with 8 bits. How many bytes are needed for the recording?
  1. c) Compare using 2 bits per sample with using 8 bits per sample. How many different levels does each allow, and how does this affect how closely the digital recording matches the original analog sound?
Solve