CSC 161 | Grinnell College | Spring, 2009 |
Imperative Problem Solving and Data Structures | ||
This laboratory exercise provides practice with
This lab utilizes two programs for processing integer values:
~walker/c/fluency-book/integer-rep.c is written in the C programming language and utilizes fixed-size-storage (both short and long integers)
~walker/scheme/fluency-book/integer-rep.ss is written in the Scheme programming language and utilizes variable-size-storage.
You should read Binary Representation of Integers in Java by Christine Wright and Ramuel Rebelsky, with revisions by Marge Coahran.
Your reading described how positive integers are stored within a computer. This part of the lab asks you to apply your understanding from this reading to both positive and negative integers, as observed on a computer.
The first several experiments in this lab utilize a program integer-rep. This program is written in the C programming language. In this language (and in most others), integers are stored using a fixed-size-storage approach, as described in the reading.
Copy ~walker/c/fluency-book/integer-rep.c to your account and compile it. Review the program listing, and write a paragraph describing how the program works.
Run integer-rep, and use the operation "A" to add 1 to the values. Then use "A" again, and again. (When starting with the value 1, the integers will become 2, 3, and 4.)
Review the binary representation of each integer, and explain why it has the binary representation printed.
Use the "M" option 3 times, each time multiplying the values by 2. The integers will become 8, 16, and 32. Explain why the binary representation of each integer looks as it does.
Use the "I" option to set the values to 5. Explain the binary representation that results.
Use the "I" option to set the values to 32. Then use the "M" option several times to multiply by 2 to obtain the values 64, 128, 256, 512, 1024, 2048, 4096, 8192, and 16384. Explain how the pattern of 0's and 1's obtained changes as you go from one of these numbers to the next.
Now that we have some experience with non-negative integers, we look at a few negative numbers.
Use the "I" option to set the program's values to -1, and describe the binary representation that you see.
Use the "M" option to multiply the program's values by 2 several times to obtain the values -2, -4, -8, -16, ..., -8192, -16384, -32768. Describe a pattern in the 0's and 1's obtained for these numbers.
Use the "I" option to enter several more negative integers and several more positive integers. Explaining the meaning of the initial (leading) bit in the fixed-size-storage approach for integers.
Use the "I" option to enter -32768 again. Then use the "S" option to subtract 1 from -32768. What result do you get with the 16-bit integer form and with the 32-bit integer? Explain why you get each result.
Use the "A" option to add 1 to your result of step 9. What can you conclude about the maximum integer that can be stored in 16 bits (assuming the processing is allowed to handle both negative and positive numbers)?
Use a similar approach to find the maximum integer that can be stored in a 32-bit (signed) integer. Explain your result and how you got it.
What happens if you try to use the "I" option to set integer values higher than this maximum for 32-bit (signed) integers? Describe what happens in at least a few test cases.
Read the news account of the computer-related difficulties that grounded all of Comair's flights on December 25, 2004. The article states, the computer system for Comair "has a hard limit of 32,000 changes in a single month." Other articles confirmed that this problem was due to a field in a database that was designed as a 16-bit integer.
What do you think was the real (not rounded) limit for changes to crews at Comair?
From what you know about the fixed-storage-approach for storing integers, identify one or two ways this problem could have been avoided.
While integer-rep.c illustrates how integers usually are stored in computers, a few environments utilize the variable-size-storage approach. The Scheme programming language and environment illustrates this alternative approach. With variable-size-storage, the binary representation does not have a 16-bit or 32-bit form; rather, the binary representation uses as many bits as needed.
Run ~walker/scheme/fluency-book/integer-rep, written in Scheme.
This program has much the same interface as the C version you used
above.
Check that the operations for entering integers, addition, subtraction,
multiplication, and division work as they did with the C version:
Use the "I" option to enter very large positive or negative numbers (e.g., over 20 digits). For example, try the values that you used in step 12, and then try even larger numbers. Explain what happens.
With your large integers from step 15, use the "A", "S", "M", and "D" options to determine if simple arithmetic still works for these large values, and describe your results.
Review your experiments with both the fixed-size-approach and the variable-size-approach for storing integers, and write a paragraph summarizing the observations you have made.
http://www.walker.cs.grinnell.edu/courses/161.sp09/labs/lab-integer-proc.shtml
created 14 January 2005 last revised 11 February 2009
|
![]() ![]() |