C++ for loop 32 bit representation problem

Having trouble understanding for loop and 32 bit representation

Page 1 of 1

1 Replies - 3568 Views - Last Post: 28 March 2008 - 02:01 PM Rate Topic: -----

#1 a2008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-March 08

C++ for loop 32 bit representation problem

Post icon  Posted 25 March 2008 - 08:54 PM

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 is this:

/****************************************************************
* 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 am completely lost. I can do all of the setup part and make it look like the example but I dont know how to get all of the zero's and ones and/or break it up into the segments. I am stuck on the for loop too. I also cannot work out what the skeleton prog is supposed to do and how we are supposed to use it?
I do not expect anyone to do this for me I just need to know how to do it. If you are able to show me how to make this work i would really appreciate it.
Can anyone help me??? Please??

Is This A Good Question/Topic? 0
  • +

Replies To: C++ for loop 32 bit representation problem

#2 jeronimo0d0a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 03-March 08

Re: C++ for loop 32 bit representation problem

Posted 28 March 2008 - 02:01 PM

You can use a union pretty well, because it can be accessed as a float or a bit field.

union			   /* Defines a two-dimensional */
{				   /*  array named screen */
	  unsigned int one : 1;  
	  unsigned int two : 1;  
	  unsigned int three : 1;  
	  unsigned int four : 1;  
	  int val;
};



assigning to val will populate the one, two... bit fields.
read about unions and bit fields.
Good Skill
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1