Ch 2. Internal Data Representation of Characters, Integers, Real Numbers

Ch 2. Practice

2.1 ASCII, EBCDIC and Unicode

Basic Skills

  1. What is the ASCII decimal value for the lowercase letter ‘k’?
  2. Which character encoding systems support the largest number of characters?
  3. How many bits are used in standard ASCII encoding?
  4. Which Unicode encoding format is most used on the web and supports variable-length encoding?
  5. Is EBCDIC compatible with ASCII and widely used in modern web applications? Explain.

Applications

  1. A program needs to store the message "KPU" using ASCII encoding.
    1. What are the ASCII decimal values for each character?
    2. How many bytes will this message occupy in memory?
  2. A file created on an IBM mainframe using EBCDIC encoding is transferred to a modern web server that uses UTF-8.
    1. What issues might arise when trying to read the file?
    2. How can these issues be resolved?
  3. You are developing a chat application that supports English, Korean, and emojis.
    1. Which character encoding system should you use and why?
    2. How many bytes might a single message like "안녕😛 " require in UTF-8?
  4. A developer is building a tool that converts characters to their ASCII values.
    1. What would be the output for the string "Data123"?
    2. How could this tool be extended to support Unicode characters?
  5. A user reports that their name "José" appears as "José" in a web form.
    1. What is the likely cause of this issue?
    2. How can the system be updated to handle such characters correctly?

Challenge Problems

  1. A string contains 100 characters, including English letters, Chinese characters, and emojis.
    1. Estimate the total number of bytes required to store this string in ASCII, UTF-8, and UTF-16.
    2. Which encoding is most space-efficient for this multilingual string, and why?
  2. A memory dump shows the following binary sequence: 01001000 01100101 01101100 01101100 01101111
    1. Decode this sequence using ASCII.
    2. What would happen if this sequence were interpreted using EBCDIC?
  3. A web application stores user input in UTF-8 but reads it back assuming ISO-8859-1 encoding.
    1. Describe a scenario where this mismatch causes corrupted output.
    2. Propose a solution to ensure consistent encoding and decoding.
  4. 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).
    1. How many bits per character would you need?
    2. Design a compact encoding scheme and explain how it compares to ASCII and UTF-8 in terms of space efficiency.
  5. 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).
    1. Explain how this affects string comparison and searching.
    2. How can a system ensure consistent handling of such characters?

 

2.2 Non-negative and Negative Integers, and Arithmetic Operations on Integers

Basic Skills

  1. What is the binary representation using two’s complement to represent -5 in 4 bits?
  2. In sign-magnitude representation, how is the number -6 represented in 4 bits?
  3. What is the range of values that a 4-bit unsigned binary number can represent?
  4. What is the result of adding the 4-bit two’s complement numbers 0110 (6) and 1010 (-6)?
  5. What does two’s complement representation simplify?

Applications

  1. A banking system stores account balances using 16-bit two’s complement integers.
    1. What is the maximum and minimum balance that can be stored?
    2. What happens if a deposit causes the balance to exceed the maximum value?
  2. A temperature sensor logs values from -40°C to +85°C. The system uses 8-bit two’s complement representation.
    1. Is this range sufficient?
    2. What binary value represents -1°C?
  3. 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?
  4. A game tracks player scores using 4-bit signed integers. A player currently has a score of -3 and earns 5 points.
    1. Represent both numbers in 4-bit two’s complement.
    2. Perform the binary addition and interpret the result.

Challenge Problems

  1. You are to convert the decimal number -45 into its 8-bit two’s complement binary representation.
    1. Show all steps: convert to binary, invert bits, and add 1.
    2. Verify your result by converting it back to decimal.
  2. You are to use 8-bit binary to compute 37 - 89 using two’s complement arithmetic.
    1. Show how to represent both numbers.
    2. Perform the binary addition and interpret the result.
    3. Explain how the system handles negative results.
  3. Compare how the number -18 is represented in 8-bit sign-magnitude and two’s complement formats as follows:
    1. Which format is more efficient for arithmetic operations and why?
    2. What are the implications for hardware design?
  4. You are designing a 6-bit signed integer format for a microcontroller.
    1. What is the range of values it can represent using two’s complement?
    2. How would you represent -17 and +17 in this format?
    3. What happens if you try to store -33?

 

2.3 Real Numbers and Floating-Point Representation of Real Numbers

Basic Skills

  1. What are the components of the IEEE 754 floating-point format?
  2. In IEEE 754 single-precision (32-bit) format, how many bits are used for the exponent?
  3. What is the binary representation of the integer part of the number 34.8?
  4. What is the purpose of the exponent in floating-point representation?
  5. What real numbers can a floating-point number represent exactly in binary?

Applications

  1. A smart thermostat records temperatures like 23.7°C and 18.2°C.
    1. How would these values be stored using IEEE 754 single-precision format?
    2. Why is floating-point representation preferred over integers in this case?
  2. A physics simulation calculates distances in space ranging from [latex]10^{-9}[/latex] to [latex]10^{9}[/latex] meters.
    1. Why is floating-point representation necessary for this application?
    2. What are the risks of using single-precision instead of double-precision?
  3. A financial app converts currency values like 1.2345 USD to CAD.
    1. What challenges might arise from using floating-point numbers for currency?
    2. Suggest a strategy to minimize rounding errors in financial calculations.
  4. A memory viewer shows the 32-bit binary pattern: 01000001010000000000000000000000
    1. Interpret this as an IEEE 754 single-precision floating-point number.
    2. What real-world value might this represent?
  5. A graphics engine uses floating-point numbers to represent pixel brightness between 0.0 and 1.0.
    1. What happens if a calculation results in a value like 1.0000001?
    2. How should the system handle values that exceed the representable range?

Challenge Problems

  1. You are to convert the decimal number -13.625 into its IEEE 754 single-precision (32-bit) floating-point binary representation.
    1. Show all steps: sign bit, binary conversion, normalization, exponent with bias, and mantissa.
    2. Verify your result by converting it back to decimal.
  2. A program stores the value 0.1 in IEEE 754 single-precision format.
    1. Explain why this value cannot be represented exactly in binary.
    2. What is the actual binary approximation stored, and what is the resulting decimal value?
  3. A calculation in a program gives the result 0.1 + 0.2 == 0.3 as false.
    1. Explain why this happens in floating-point arithmetic.
    2. Suggest a reliable method to compare floating-point numbers in software.
  4. Answer the following:
    1. What is the smallest positive non-zero value that can be represented in IEEE 754 single-precision format?
    2. Explain how denormalized numbers are used to represent values smaller than the smallest normalized number.
    3. Why is this important in scientific computing?
  5. A scientific application calculates values that exceed [latex]3.4 \times 10^{38}[/latex], the maximum for IEEE 754 single-precision.
    1. What happens when such a value is computed?
    2. How can the application be modified to handle such large values safely?

 

2.4 Data Underflow, Overflow, and Interpreting Memory Content

Basic Skills

  1. What is overflow in binary arithmetic?
  2. What is the result of adding 111112 (31 in decimal) and 000012 (1 in decimal) using 5-bit unsigned integers?
  3. When does underflow occur in floating-point arithmetic?
  4. What is the largest positive denormalized number in IEEE 754 single-precision format approximately equal to?
  5. Name three different values that the same 16-bit pattern could represent.

Applications

  1. A banking application uses 16-bit signed integers to store account balances. A customer has a balance of $23,765 and deposits $10,000.
    1. What will happen if the system adds the deposit without checking for overflow?
    2. How can the system detect and prevent this issue?
  2. A binary file is transferred between two systems with different endianness (byte order).
    1. How might this affect the interpretation of multi-byte integers or floating-point values?
    2. What strategies can be used to ensure consistent interpretation across platforms?
  3. A developer notices that a floating-point calculation unexpectedly returns zero. The input value was [latex]1.0 \times 10^{-45}[/latex].
    1. Explain why this might happen in IEEE 754 single-precision.
    2. How can the developer adjust the code or data type to avoid this issue?

Challenge Problems

  1. A system uses 8-bit two’s complement integers. A developer adds 100 and 60.
    1. Show the binary addition.
    2. Explain why the result is incorrect.
    3. How can the system detect and handle this overflow?
  2.  A scientific application computes a value of [latex]1.0 \times 10^{-46}[/latex] using IEEE 754 single-precision.
    1. Determine whether this value is representable.
    2. If not, explain how denormalized numbers attempt to represent it.
    3. What are the trade-offs of using denormalized numbers?
  3. A 32-bit binary pattern 01000001010010000000000000000000 is stored in memory.
    1. Interpret this pattern as a signed number, a floating-point number, and a character array.
    2. Discuss how misinterpreting the data type could lead to bugs or security issues.
  4. A 32-bit integer 0x23456789 is stored in memory.
    1. Show how this value is stored in both big-endian and little-endian formats.
    2. What problems might arise when transferring this data between systems with different endianness?
    3. How can software ensure consistent interpretation?
  5. 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).
    1. Explain how underflow or rounding errors could affect the total.
    2. Propose a better data representation strategy to ensure accuracy.