In: Computer Science
Language: C++ In your main(), use printf() to print out the floating point values for some hex data. a. To print 1.0, do printf("One: %f\n",0x3FF0000000000000); The compiler will give you a warning about the argument being a different type than the format, but that is ok. b. To print 2.0, do printf("Two: %f\n",0x4000000000000000); Remember, to multiply by two, you just add one to the exponent. c. Print 4.0, 8.0, and 16.0 (with nice labels, of course). d. We can also go the other way. To divide by two, just decreas the exponent by one. So for 1/2, do printf("Half: %f\n",0x3FE0000000000000); e. Print 1/4, 1/8, 1/16. f. Negative values have a 1 in the leading bit instead of 0. A leading 1 in the bits for a hex digit has value 8, so -1.0 is BFF0000000000000. So to print -1.0, do printf("Neg One: %f\n",0xBFF0000000000000); g. Print -2, -4, -8, -1/2, -1/4, -1/8 (with nice labels, of course).
Comment if there are any issues, queries or if you need any changes to be made.
Code:
#include <iostream> int main() { printf("One: %f\n", 0x3FF0000000000000); printf("Two: %f\n", 0x4000000000000000); //401 is the new exponent //4 in hex is 0x4010000000000000 printf("Four: %f\n", 0x4010000000000000); //exponent 402 printf("Eight: %f\n", 0x4020000000000000); //exponent 403 printf("Sixteeen: %f\n", 0x4030000000000000); //print 1/2 printf("Half: %f\n", 0x3FE0000000000000); //subtract 1 from exponent of 1/2 //new exponent 3FD for 1/4. printf("a Fourth: %f\n", 0x3FD0000000000000); // 1/8 subtract 1 from exponent of 1/4 printf("an Eighth %f\n", 0x3FC0000000000000); // 1/16 subtract 1 from exponent of 1/8 printf("one Sixteenth %f\n", 0x3FB0000000000000); //-1 printf("Neg One: %f\n", 0xBFF0000000000000); //-2 printf("Neg Two: %f\n", 0xC000000000000000); //-4, add 1 to exponent of -2 printf("Neg Four: %f\n", 0xC010000000000000); //-8, add 1 to exponent of -4 printf("Neg Eight: %f\n", 0xC020000000000000); // -1/2 printf("Neg Half: %f\n", 0xBFE0000000000000); // -1/4 printf("Neg a Fourth: %f\n", 0xBFD0000000000000); // -1/8 printf("Neg an Eighth: %f\n", 0xBFC0000000000000); return 0; }
Code Screenshot:
Output: