Hi,
I have a crazy assignment that I'm trying to figure out. I know that it can be done fairly simply in C++ with union structs. Since we have no restriction on the language, I of course chose my favorite, PHP. Here's a rundown:
For this assignment you will write a program that reads float numbers from stdin and displays the sign, exponent, and mantissa parts of their floating point representation. The input should contain one floating point number per line. For each input number the output should have the following information:
* the input number
* the bits of the floating point number represented in hexadecimal
* the sign bit of the number
* the exponent bits of the number represented in hexadecimal
* the mantissa bits of the number represented in hexadecimal
For example, for the input numbers -1.75 and 23.5 your output could look like
number: -1.75000
bits: BFE00000
sign: 1
exponent: 7F
mantissa: 600000
number: 23.50000
bits: 41BC0000
sign: 0
exponent: 83
mantissa: 3C0000
For those that don't know, in a 32-bit binary representation of a float the sign is determined by the leftmost bit, position 31 (0 for positive, 1 for negative); the exponent is contained in bits 30-23, the mantissa (or fractional portion) is the portion of the float that follows the decimal in positions 22-0.
Sign 1 [31]
Exponent 8 [30-23]
Fraction 23 [22-00]
I've played around with the PEAR library, Math_BinaryUtils, but haven't had much luck. Can anyone point me in the right direction?
The main thing is getting the float into a binary representation. If I can get that far, I think I have the tools to do everything else.
Thanks in advance!