Homework help please!! Desperate!! For loops and binary representation

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:

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 // for cin and cout
#include // 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 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.
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.

I tried searching for info on the net 'c++ help', 'for loop', 'for loop c++', 'binary representation c++', 'binary representation for loop', and alot more (in google and yahoo searches). I cannot find anything simple enough for me to understand.

As far as i know i can only add to the int main on the skeletal program. Not really sure. The main problem is that 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. I have done all of the formatting and the prompt for real number etc, I am just stuck on the 0's and 1's parts. It has just completely gone over my head.

Please help. I am really desperate now and running out of time.

Thank you......
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories