Ch 2. Internal Data Representation of Characters, Integers, Real Numbers
Ch 2. Practice
2.1 ASCII, EBCDIC and Unicode
Basic Skills
- What is the ASCII decimal value for the lowercase letter ‘k’?
- Which character encoding systems support the largest number of characters?
- How many bits are used in standard ASCII encoding?
- Which Unicode encoding format is most used on the web and supports variable-length encoding?
- Is EBCDIC compatible with ASCII and widely used in modern web applications? Explain.
Applications
- A program needs to store the message "
KPU" using ASCII encoding.- What are the ASCII decimal values for each character?
- How many bytes will this message occupy in memory?
- A file created on an IBM mainframe using EBCDIC encoding is transferred to a modern web server that uses UTF-8.
- What issues might arise when trying to read the file?
- How can these issues be resolved?
- You are developing a chat application that supports English, Korean, and emojis.
- Which character encoding system should you use and why?
- How many bytes might a single message like "안녕😛 " require in UTF-8?
- A developer is building a tool that converts characters to their ASCII values.
- What would be the output for the string "
Data123"? - How could this tool be extended to support Unicode characters?
- What would be the output for the string "
- A user reports that their name "
José" appears as "José" in a web form.- What is the likely cause of this issue?
- How can the system be updated to handle such characters correctly?
Challenge Problems
- A string contains 100 characters, including English letters, Chinese characters, and emojis.
- Estimate the total number of bytes required to store this string in ASCII, UTF-8, and UTF-16.
- Which encoding is most space-efficient for this multilingual string, and why?
- A memory dump shows the following binary sequence:
01001000 01100101 01101100 01101100 01101111- Decode this sequence using ASCII.
- What would happen if this sequence were interpreted using EBCDIC?
- A web application stores user input in UTF-8 but reads it back assuming ISO-8859-1 encoding.
- Describe a scenario where this mismatch causes corrupted output.
- Propose a solution to ensure consistent encoding and decoding.
- Suppose you are designing a lightweight encoding system for a device that only needs to support the characters: A to Z, a to z, 0 to 9, and a few symbols (e.g., space, period, comma).
- How many bits per character would you need?
- Design a compact encoding scheme and explain how it compares to ASCII and UTF-8 in terms of space efficiency.
- The character “é” can be represented in Unicode as either a single code point (U+00E9) or as a combination of “e” (U+0065) and an acute accent (U+0301).
- Explain how this affects string comparison and searching.
- How can a system ensure consistent handling of such characters?
2.2 Non-negative and Negative Integers, and Arithmetic Operations on Integers
Basic Skills
- What is the binary representation using two’s complement to represent -5 in 4 bits?
- In sign-magnitude representation, how is the number -6 represented in 4 bits?
- What is the range of values that a 4-bit unsigned binary number can represent?
- What is the result of adding the 4-bit two’s complement numbers
0110(6) and1010(-6)? - What does two’s complement representation simplify?
Applications
- A banking system stores account balances using 16-bit two’s complement integers.
- What is the maximum and minimum balance that can be stored?
- What happens if a deposit causes the balance to exceed the maximum value?
- A temperature sensor logs values from -40°C to +85°C. The system uses 8-bit two’s complement representation.
- Is this range sufficient?
- What binary value represents -1°C?
- A digital clock uses unsigned 6-bit binary numbers to represent minutes (0 to 59). What happens if the clock tries to add 5 minutes to 58?
- A game tracks player scores using 4-bit signed integers. A player currently has a score of -3 and earns 5 points.
- Represent both numbers in 4-bit two’s complement.
- Perform the binary addition and interpret the result.
Challenge Problems
- You are to convert the decimal number -45 into its 8-bit two’s complement binary representation.
- Show all steps: convert to binary, invert bits, and add 1.
- Verify your result by converting it back to decimal.
- You are to use 8-bit binary to compute 37 - 89 using two’s complement arithmetic.
- Show how to represent both numbers.
- Perform the binary addition and interpret the result.
- Explain how the system handles negative results.
- Compare how the number -18 is represented in 8-bit sign-magnitude and two’s complement formats as follows:
- Which format is more efficient for arithmetic operations and why?
- What are the implications for hardware design?
- You are designing a 6-bit signed integer format for a microcontroller.
- What is the range of values it can represent using two’s complement?
- How would you represent -17 and +17 in this format?
- What happens if you try to store -33?
2.3 Real Numbers and Floating-Point Representation of Real Numbers
Basic Skills
- What are the components of the IEEE 754 floating-point format?
- In IEEE 754 single-precision (32-bit) format, how many bits are used for the exponent?
- What is the binary representation of the integer part of the number 34.8?
- What is the purpose of the exponent in floating-point representation?
- What real numbers can a floating-point number represent exactly in binary?
Applications
- A smart thermostat records temperatures like 23.7°C and 18.2°C.
- How would these values be stored using IEEE 754 single-precision format?
- Why is floating-point representation preferred over integers in this case?
- A physics simulation calculates distances in space ranging from [latex]10^{-9}[/latex] to [latex]10^{9}[/latex] meters.
- Why is floating-point representation necessary for this application?
- What are the risks of using single-precision instead of double-precision?
- A financial app converts currency values like 1.2345 USD to CAD.
- What challenges might arise from using floating-point numbers for currency?
- Suggest a strategy to minimize rounding errors in financial calculations.
- A memory viewer shows the 32-bit binary pattern:
01000001010000000000000000000000- Interpret this as an IEEE 754 single-precision floating-point number.
- What real-world value might this represent?
- A graphics engine uses floating-point numbers to represent pixel brightness between 0.0 and 1.0.
- What happens if a calculation results in a value like 1.0000001?
- How should the system handle values that exceed the representable range?
Challenge Problems
- You are to convert the decimal number -13.625 into its IEEE 754 single-precision (32-bit) floating-point binary representation.
- Show all steps: sign bit, binary conversion, normalization, exponent with bias, and mantissa.
- Verify your result by converting it back to decimal.
- A program stores the value 0.1 in IEEE 754 single-precision format.
- Explain why this value cannot be represented exactly in binary.
- What is the actual binary approximation stored, and what is the resulting decimal value?
- A calculation in a program gives the result
0.1 + 0.2 == 0.3asfalse.- Explain why this happens in floating-point arithmetic.
- Suggest a reliable method to compare floating-point numbers in software.
- Answer the following:
- What is the smallest positive non-zero value that can be represented in IEEE 754 single-precision format?
- Explain how denormalized numbers are used to represent values smaller than the smallest normalized number.
- Why is this important in scientific computing?
- A scientific application calculates values that exceed [latex]3.4 \times 10^{38}[/latex], the maximum for IEEE 754 single-precision.
- What happens when such a value is computed?
- How can the application be modified to handle such large values safely?
2.4 Data Underflow, Overflow, and Interpreting Memory Content
Basic Skills
- What is overflow in binary arithmetic?
- What is the result of adding 111112 (31 in decimal) and 000012 (1 in decimal) using 5-bit unsigned integers?
- When does underflow occur in floating-point arithmetic?
- What is the largest positive denormalized number in IEEE 754 single-precision format approximately equal to?
- Name three different values that the same 16-bit pattern could represent.
Applications
- A banking application uses 16-bit signed integers to store account balances. A customer has a balance of $23,765 and deposits $10,000.
- What will happen if the system adds the deposit without checking for overflow?
- How can the system detect and prevent this issue?
- A binary file is transferred between two systems with different endianness (byte order).
- How might this affect the interpretation of multi-byte integers or floating-point values?
- What strategies can be used to ensure consistent interpretation across platforms?
- A developer notices that a floating-point calculation unexpectedly returns zero. The input value was [latex]1.0 \times 10^{-45}[/latex].
- Explain why this might happen in IEEE 754 single-precision.
- How can the developer adjust the code or data type to avoid this issue?
Challenge Problems
- A system uses 8-bit two’s complement integers. A developer adds 100 and 60.
- Show the binary addition.
- Explain why the result is incorrect.
- How can the system detect and handle this overflow?
- A scientific application computes a value of [latex]1.0 \times 10^{-46}[/latex] using IEEE 754 single-precision.
- Determine whether this value is representable.
- If not, explain how denormalized numbers attempt to represent it.
- What are the trade-offs of using denormalized numbers?
- A 32-bit binary pattern
01000001010010000000000000000000is stored in memory.- Interpret this pattern as a signed number, a floating-point number, and a character array.
- Discuss how misinterpreting the data type could lead to bugs or security issues.
- A 32-bit integer
0x23456789is stored in memory.- Show how this value is stored in both big-endian and little-endian formats.
- What problems might arise when transferring this data between systems with different endianness?
- How can software ensure consistent interpretation?
- A financial system uses 32-bit floating-point numbers to store currency values. A high-volume merchant receives thousands of microtransactions (e.g., $0.0000001).
- Explain how underflow or rounding errors could affect the total.
- Propose a better data representation strategy to ensure accuracy.