CSCI 463 - Assignment 2
Bitwise Operators & IEEE-754 Floating Point Number Decoding

Read 32-bit HEX representations of IEEE-754 Floating Point Numbers, decode them, and print their componenets.

Setup

Using the prior assignment as a reference, create a directory for this assignment and a suitable Makefile that will compile a program called prog2 from a source file named prog2.cpp.

Files You Must Write

prog2.cpp

Create a single-file application that will read zero or more 32-bit hex values and then print a detailed description of the floating point value that they represent by extracting and showing the sign, exponent, and significand from the 32-bit value in the manner described below.

This file must contain (at least) two functions: main() and printBinFloat(uint32_t x). Your main must have a read-loop that calls printBinFloat to do the decoding and printing.

Sample Test Files

NOTE: In order to receive full credit, your program is expected to generate precisely indentical output in the exact same format (space for space) as the output file given above.

See below for a discussion on how to compare yours against the given reference.

Input

Read your input from stdin (aka std::cin).

To read a 32-bit hexadecimal value from stdin use the std::hex manipulator with the std::cin stream as shown below:

uint32_t  x;

std::cin >> std::hex >> x;

Output

Write your output to stdout (aka std::cout).

printBinFloat must format and print each value precisely in the following manner.

0x3f800000 = 0011 1111 1000 0000 0000 0000 0000 0000
sign: 0
 exp: 0x00000000 (0)
 sig: 0x00000000
+1.00000000000000000000000

In this particular example, the hex value 3f800000 was read and the values of its fields are shown as well as its actual binary value.

For each input value, the five output lines printed are:

Positive infinity must be displayed like this: +inf and negative infinity: -inf as shown below:

0x7f800000 = 0111 1111 1000 0000 0000 0000 0000 0000
sign: 0
 exp: 0x00000080 (128)
 sig: 0x00000000
+inf
0xff800000 = 1111 1111 1000 0000 0000 0000 0000 0000
sign: 1
 exp: 0x00000080 (128)
 sig: 0x00000000
-inf

The positive and negative zero values must be printed like this:

0x00000000 = 0000 0000 0000 0000 0000 0000 0000 0000
sign: 0
 exp: 0xffffff81 (-127)
 sig: 0x00000000
+0
0x80000000 = 1000 0000 0000 0000 0000 0000 0000 0000
sign: 1
 exp: 0xffffff81 (-127)
 sig: 0x00000000
-0

Some other examples show how to display values with larger exponents:

0xbf400001 = 1011 1111 0100 0000 0000 0000 0000 0001
sign: 1
 exp: 0xffffffff (-1)
 sig: 0x00400001
-0.110000000000000000000001
0xf7ffffff = 1111 0111 1111 1111 1111 1111 1111 1111
sign: 1
 exp: 0x00000070 (112)
 sig: 0x007fffff
-11111111111111111111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0
0x00ffffff = 0000 0000 1111 1111 1111 1111 1111 1111
sign: 0
 exp: 0xffffff82 (-126)
 sig: 0x007fffff
+0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111

Documentation

Document your code using the Doxygen rules as described in lecture, course syllabus, and links that can be found on the CSCI 463 faculty page where you found this assignment.

You will lose ALL your documentation points if you do not provide proper Doxygen format including, when appropriate, the @param, @return, and other things as discussed in lecture.

Handing In Your Assignment

Hand in your assignment by using the mailprog.463 script in the manner described in lecture.

Grading

We will compile your program on hopper EXACTLY like this:

g++ -Wall -Werror -std=c++11 prog2.cpp -o prog2

If the above compilation fails to create an executable file then you will receive zero points for the assignment.

If the compilation succeeds then your program will be run and its output compared against the reference output EXACTLY like this:

./prog2 < grade-data.in > student.out
diff grade-data.out student.out

If there are ANY lines of output that are printed from the diff command then your output is wrong and a large portion (possibly all) of your output grade points will be deducted.

You will lose many coding points if you read your input data or write your output data in manner that is messier than the clear and simple methods that are discussed in detail in the "Reading, Printing, and Formatting Numbers in C++" review lectures on the course web page.

Your program will be graded using different test data than that given in the assignment handouts. Your program MUST be able to handle reading an empty file as well as files with varying numbers of data values.

Hints

Last modified: 2022-08-18 13:52:50 CDT