GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Mar-2008, 13:44
un2008 un2008 is offline
New Member
 
Join Date: Mar 2008
Posts: 1
un2008 is on a distinguished road

Binary output


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:
CPP / C++ / C 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 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.

Thank you.
Last edited by admin : 26-Mar-2008 at 18:19. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 26-Mar-2008, 15:44
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 400
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: C++ Help Please. Binary output


Assuming that your TestBit() function is working, here is how you would write a loop:

CPP / C++ / C Code:
unsigned i;
float f = 2.25;

for(i=0;i<32;++i) cout << TestBit(i,f);
  #3  
Old 26-Mar-2008, 18:55
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 353
Peter_APIIT is on a distinguished road

Re: Binary output


I wonder how to output binary number instead of integer.

Thanks.
__________________
Linux is the best OS in the world.
  #4  
Old 27-Mar-2008, 07:34
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 400
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Binary output


Quote:
Originally Posted by Peter_APIIT
I wonder how to output binary number instead of integer.

Thanks.
I wrote this function some time ago. It converts a byte to a binary string. For example, it would convert 0xF3 to "1111 0011".
CPP / C++ / C Code:
string ByteToString( unsigned char byte )
{	int i;
	string result = "";
	unsigned char mask = 0x80;

	for(i=0;i<8;i++)
	{	result = result + ((byte&mask)? "1" : "0");
		mask >>= 1;
		if(i == 3) result = result + " ";
	}
	return result;
}
I'm sure that it could easily be changed to handle integers instead of bytes.
  #5  
Old 27-Mar-2008, 09:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,527
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Binary output


Quote:
Originally Posted by un2008
... I do not understand how to write a for loop into it or return the particular bit of the float number

I have to assume that you didn't write TestBits(). Maybe it was given to you??

Anyhow, assume for the moment that it works as "advertised" in the comments.

Then...

Since the function returns the value of a bit at position given by the value of the second argument, and, assuming that the bit positions of a floating point number are 31 (for the highest bit position) down to zero (for the lowest bit position), couldn't you look at the bits with something like the following:
CPP / C++ / C Code:
    float x = 6.0;
    cout << "The value of x is " << x << endl;
    cout << "Here are the bits:\n  ";
    for (int i = 31; i >= 0; i--) 
    {
        cout << TestBit(i, x);
    }
    cout << endl;

Try that to get a feel for it:

Code:
The value of x is 6 Here are the bits: 01000000110000000000000000000000

Now, if you want to capture the bits in an array so that you can break out the components of the floating point number (sign, exponent, etc.), couldn't you simply make the loop:

CPP / C++ / C Code:
    int bits[32];
    for (int i = 31; i >= 0; i--) 
    {
        bits[i] = TestBit(i, x);
    }

Then bits[31] is the sign; bits[30] through bits[23] are the exponent, etc. Right?


Regards,

Dave

Footnote: Why did I put "mantissa" between quote marks? Because it is not really the "mantissa" as usually defined for scientific notation. The official designation is, I think, "significand." But "mantissa" is commonly used although it is not technically correct. See the following for more details: http://en.wikipedia.org/wiki/Floating_point
 

Recent GIDBlogNew Corolla Altis, 10th Generation - Part I by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 52 28-Mar-2008 09:15
One int to one char* for binary output c4a CPP / C++ Forum 4 23-Nov-2007 08:24
New to programming (Homework Help) fishguts Miscellaneous Programming Forum 1 12-Sep-2007 10:50
Binary to Decimal Conversion Saberwing C Programming Language 5 28-Dec-2006 14:57
Bit stuffing agnostos C Programming Language 17 12-Sep-2006 20:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:20.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.