Hi
I am trying to write a program that displays all the bits of the binary representation of the type float.
The program needs to display info like this Example. (User enters the number 6).
This program displays a binary representation of real numbers.
Enter a real number: 6
The number's representation is 32 bits long:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01000000110000000000000000000000
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sign: 0
exponent: 10000001
mantissa: 10000000000000000000000
Notes:
To display the bits I have to use the for loop.
There is a skeleton program which has to be used and only edited within int main:
I am completely lost. I do not understand how to write a for loop into it or return the particular bit of the float number. I have spent days trying to work it out. If anyone could help by giving me an example of how it works or showing me how to do it I would really appreciate it.Code:/**************************************************************** * skeleton program file float.cpp * This program calculate calculates and displays a binary * representation of real numbers. * Assume all input is of the correct format. * * Input: a real number. * * Output: The bit pattern of the number stored in the computer's * memory as float * * Processing: * ....... * * Author: * ********************************************************************/ #include <iostream> // for cin and cout #include <iomanip> // for setw() using namespace std; // prototype int TestBit( unsigned bit, float number ); // start of the program int main() { return 0; } // of main() /* * TestBit: tests bits of a float number * receive: a bit where 0 is the rightmost bit * and a number to test * return: 1 if the bit is set on, 0 therwise * preconditions: bit is in the range 0..31 */ int TestBit( unsigned bit, float number ) { if ( bit < 8*sizeof(number) ) if ( ((( unsigned int) 1) << bit) & (*((unsigned int*)(&number))) ) return 1; return 0; } // TestBit
I have done all of the formatting and the prompt for real number etc (i think), I am just stuck on the 0's and 1's parts. It has just completely gone over my head.
Thank you......