![]() |
|
#1
|
|||
|
|||
Binary outputI 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:
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
|
|||
|
|||
Re: C++ Help Please. Binary outputAssuming that your TestBit() function is working, here is how you would write a loop:
CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: Binary outputI wonder how to output binary number instead of integer.
Thanks. __________________
Linux is the best OS in the world. |
|
#4
|
|||
|
|||
Re: Binary outputQuote:
CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
Re: Binary outputQuote:
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:
Try that to get a feel for it: Code:
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:
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 GIDBlog
New Corolla Altis, 10th Generation - Part I by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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