prog1.cpp Create a single-file application that will read zero or more 32-bit hex values and then print the value they represent in binary by extracting the sign, exponent, and significand from the...



prog1.cpp


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


This file should contain two functions:main()andprintBinFloat(int32_t x). Yourmainshould have a read-loop that callsprintBinFloatto do the decoding and printing.


Do not define any global or static-class variables in this assignment.


Do not define or use any floating point variables or operations assignment.



printBinFloat(int32_t x)must use the bitwise operators to extract and shift the fields of the IEEE number as needed to render the output as described below.


Input


Read your input fromstdin(akastd::cin).


To read a 32-bit hexadecimal value, use thestd::hexmanipulator along the lines of this:


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

It is your responsibility to create your own test data for this assignment.


Output



printBinFloatmust 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 example, the hex value3f800000was read and the values of its fields are shown as well as its actual value is expressed in binary. This is expressed by the fact that:



  • The first line displays the input value as a 32-bit hex value and again in binary (in groups of 4-bits.)

  • The sign bit is zero. (Therefore the floating point value is positive.)

  • The exponenet is zero (seen printed as a 32-bit signed integer and as a signed decimal value in parenthesis.)

  • The significand is printed as a 32-bit unsigned hex value. In this case it is zero.

  • The full value of the number is printed last in binary.


Positive infinity should be displayed like this:+infand negative infinity:-infas 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 should 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 test values that 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 0xffffffff = 1111 1111 1111 1111 1111 1111 1111 1111  sign: 1  exp: 0x00000080 (128)  sig: 0x007fffff -111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0 0x007fffff = 0000 0000 0111 1111 1111 1111 1111 1111  sign: 0  exp: 0xffffff81 (-127)  sig: 0x007fffff +0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111

Documentation


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


Handing In Your Assignment


Hand in your assignment by using themailprog.463script in the manner described in Assignment 0.


Hints



  • See the RVALP appendix on floating point numbers (as seen in lecture) for details on the use of the bits in a 32-bit floating point number.

  • To print one of the binary bit values of an integer variable,andthe integer with a single 1-bit in the position you want to display like this:


    uint32_t x; ... std::cout

    This will print a 1 if the MSB of the variable is a 1 and a 0 if it is 0. Obviously, you'll want to do this in a suitable loop to print out the entire integer on the first line of your output.



  • Don't even
    think
    about formatting the final binary value until you know you have extracted and properly interpreted each of thesign,exponent, andsignificandvalues! ...the easiest final values to start with are the special cases (for them, just hard-code anif-then,else-if,else-if... to detect them and print the zero or infinity values.)




  • Use a text editor that shows the character column number to view your output so you don't have to count dozens of zeros to make sure your exponents are applied properly!




  • Don't forget to account for the implied1.to the left end of the significand!!!!




  • While it should be possible to write one set of logic to print the final values of all binary numbers that have either a positive or a negative exponent, it will be much easier to use an if-then on the exponent and then write different formatting code for the positive and negative exponent cases.


    Only if you are bored and want a challenge, consider trying to merge the two into one universal formatter.



Sep 08, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here