CSC 161  Grinnell College  Spring, 2010 
Imperative Problem Solving and Data Structures  
This laboratory exercise provides practice with
This lab utilizes two programs for processing integer values:
~walker/c/fluencybook/integerrep.c is written in the C programming language and utilizes fixedsizestorage (both short and long integers)
~walker/scheme/fluencybook/integerrep.ss is written in the Scheme programming language and utilizes variablesizestorage.
You should read Binary Representation of Integers in C and Java by Christine Wright and Ramuel Rebelsky, with revisions by Marge Coahran and Henry M. Walker.
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 integerrep. This program is written in the C programming language. In this language (and in most others), integers are stored using a fixedsizestorage approach, as described in the reading.
Copy ~walker/c/fluencybook/integerrep.c to your account and compile it. Review the program listing, and write a paragraph describing how the program works.
Run integerrep, 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 nonnegative 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 fixedsizestorage 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 16bit integer form and with the 32bit 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 32bit (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 32bit (signed) integers? Describe what happens in at least a few test cases.
Read the news account of the computerrelated 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 16bit integer.
What do you think was the real (not rounded) limit for changes to crews at Comair?
From what you know about the fixedstorageapproach for storing integers, identify one or two ways this problem could have been avoided.
While integerrep.c illustrates how integers usually are stored in computers, a few environments utilize the variablesizestorage approach. The Scheme programming language and environment illustrates this alternative approach. With variablesizestorage, the binary representation does not have a 16bit or 32bit form; rather, the binary representation uses as many bits as needed.
Run ~walker/scheme/fluencybook/integerrep, 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 fixedsizeapproach and the variablesizeapproach for storing integers, and write a paragraph summarizing the observations you have made.
http://www.cs.grinnell.edu/~walker/courses/161.sp10/labs/labintegerproc.shtml
created 14 January 2005 last revised 15 February 2010
