Exercises: Binary Numbers and Overflow
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.
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.
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.
- a) How many different values can be represented with 6 bits?
- b) How many bytes are needed to store exactly 40 bits?
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$.
- a) What is the true sum, written in decimal and in binary?
- b) What happens inside the calculator, and what value does it end up storing? (Hint: think of what happens to the extra bit.)
- c) What is the name of this kind of error?
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.
- a) What is the minimum number of bits needed?
- b) If the school adds one more bit to the ID, how many total clubs can it identify?
- c) Explain in one or two sentences why adding one bit doubles the number of values.
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.
- a) Explain why this happens. Use the idea that computers store numbers using a limited number of bits.
- b) What is the name for this kind of error?
- c) Describe a safer way for a program to check whether two decimal results are "equal."
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.
- 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$.
- b) Explain why a player would see this as a bug.
- 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.
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.
- a) Explain how a computer "knows" which meaning to use for the same pattern.
- 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?
- 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?