Programming Assignment 2

Due Thursday, October 4
15 points


Overview

For this assignment you will write a program that reads float numbers from stdin and displays the sign, exponent, and mantissa parts of their floating point representation. The input should contain one floating point number per line. For each input number the output should have the following information:

For example, for the input numbers -1.75 and 23.5 your output could look like

number:     -1.75000
bits:       BFE00000
sign:              1
exponent:         7F
mantissa:     600000

number:     23.50000
bits:       41BC0000
sign:              0
exponent:         83
mantissa:     3C0000

    

Coding Suggestions

The following subsections provide suggestions for handling various aspects of the coding.

Line-by-Line Input Processing

The standard technique for processing input line-by-line is to set up a loop that uses gets() or fgets() to read a line into an array of characters. You can then process the characters directly or use sscanf() to do formatted text conversions on the input line. When there is no more input, both gets() and fgets() return NULL.

Floating Point Input

For line-by-line floating point input you can process the character string read by gets() or fgets() using sscanf() with a "%f" format.

Getting the Bits of a Float Number

You can obtain the bits of a float number using a C union. First declare a variable converter as follows:

    union {
	float fVal;
	unsigned long lVal;
    } converter;
    
This creates a single memory location that can hold either a float number, converter.fVal, or an unsigned long number, converter.lVal. Since there is just one memory location, you can put a floating point number in it by assigning a float value to converter.fVal and read out the bits by referencing converter.lVal.
Hexadecimal Output

You can output a long integer in hexadecimal using printf() or fprintf() with a "%lx" or "%lX" format specifier. For example, if l is declared as a long (either signed or unsigned) then the following statement prints l right justified in a field of width 8. At least 6 hexadecimal digits are printed.

    printf("8.6lX", l);
    

With a "%lx" format, the digits are printed using lower case letter ('a' through 'f'). With a "%lX" format, the digits are printed using lower case letter ('A' through 'F').

Hexadecimal Literal Constants

In your C code, you can specify hexadecimal literal constants with a "0x" prefix. For example, 0x43 can be used interchangably with 67. Note that 4316 = 6710.

Bit Manipulations

You can select fields of bits from an integer using a combination of bitwise shifts and bitwise and operations. If l is an integer then

The >> operator has higher precedence than the & operator.

What to Turn In

You should turn in a listing of your program along with a recording of its execution with sample input that shows the representations of floating point numbers with a variety of magnitudes and signs. Your program listing should contain your name and the course number. Your code should be commented with at least a description of what it does and it should have comments for each of the functions.