CST 8110 - Assignment Eleven

Last revised: Wednesday, July 9, 1997.

Only the input/output of this program is due in my assignment box by 13:45pm Monday, July 14.

All assignments are evaluated based on their neatness and how closely they follow the assignment submission guidelines published in the online course notes.

Purpose and Instructions

This program shows the complex internal format of floating-point numbers.

Write a program that reads in a floating-point number and prints, in hexadecimal, the bit pattern stored in memory. The number read in will be a double, which is eight bytes (64 bits, or 16 4-bit hex digits) wide.

Since printf's %x format only prints 2 bytes (the size of an int), and %lx only prints 4 bytes (the size of a long int), we need to make two calls to %lx to read all 2x4=8 of the bytes of the double, with each %lx handling 4x8=32 of the 64 bits.

   double dub;     /* double - 64 bits, 16 hex digits, 8 bytes */
   printf("%f is stored as %lx %lx in memory.\n", dub, dub);

To improve the look of the output, and so that we better know what bits are set, we can improve the print statement above.

Let us adjust the field width of the printf formats to print each of the two 32-bit halves of the double using all 8 character positions (8 positions means 8 hex digits, which means 4x8=32 bits). Also use printf's leading-'0' format that makes numbers print with all the leading zeroes, so that we see which bits are zeroes. Lastly, we can use the fancy printf '%g' format to print the floating-point number (especially very small numbers such as 1.0e-30) in a more readable manner:

   double dub;
   printf("%g is stored as %08lx%08lx in memory.\n", dub, dub);

The two uses of '%08lx' print 8+8=16 hex digits, which corresponds to all 8 bytes of the double given as the argument.

Hand in: A sheet containing the inputs to and outputs from your program for the following test data. You need to run your program for each of the input values and cut/paste the output into a word processing program to print and hand in.

(Positive Numbers)
Output?   Input
(Negative Numbers)
0.0     0.0  
1.0     -1.0  
2.0     -2.0  
3.0     -3.0  
10.0     -10.0  
11.0     -11.0  
12.0     -12.0  
13.0     -13.0  
100.0     -100.0  
101.0     -101.0  
102.0     -102.0  
103.0     -103.0  
1.0e30     -1.0e30  
2.0e30     -2.0e30  
3.0e30     -3.0e30  
1.0e-30     -1.0e-30  
2.0e-30     -2.0e-30  
3.0e-30     -3.0e-30  

Sample Output

The hex bit patterns will be quite messy, since for floating-point numbers we are looking at a combined mantissa and exponent each stored in a special format. Here are two of the output data values (from an IBM PC running Borland 4.52):

     1.0 is stored as 000000003ff00000 in memory.
   103.0 is stored as 000000004059c000 in memory.

If you run your program on other systems than the PC, you may get different bit patterns. Don't expect to see the hexadecimal equivalent of the number you type in -- the floating-point conversion converts the number into a mantissa and exponent, and that conversion significantly alters the appearance of the bits in memory.

As always, the program must follow Algonquin standards for headers and format. The algorithm you design or use to solve the problem in each header must use pseudocode where appropriate. You do not have to hand in the code for this assignment. Hand in only the input and output.

Advanced Programming - (not required for this assignment)

We haven't covered looping yet, but here's an example of how to construct a simple loop that will make your program keep asking for numbers until the number "123" is entered. To use this, the example given below needs to be modified to work with floating-point values, and it needs to have the correct printf statement inserted in the loop body:

   #define SENTINEL 123
   do { 
      printf("Enter a number (%d to quit) >>>", SENTINEL);
      printf("You entered %d\n", num);
   } while( num != SENTINEL );

You may find using this looping easier than re-running your program each time.

Try running your program using %.50f instead of %f or %g for the first format place-holder in the printf. Try entering multiples of one tenth (0.1) to see what the bits look like in memory. Try as input some powers of two, e.g. 2, 4, 8, 16, 512, 1024, 32768, etc.; they follow a more regular pattern in memory than decimal numbers.

Question: Is an integer zero the same bit pattern as a floating-point zero?


The aggregate of all assignment marks comprises 25% of your final mark. All assignments must be completed satisfactorily to get credit for the course, even if the assignments are submitted too late to receive a mark. (See the course outline.)

Late assignments are handled according to the policy given in the course outline.