Link to home
Start Free TrialLog in
Avatar of fisham29
fisham29

asked on

Representing Floats in Hex

This is not exactly a C question (I'm actually stuck upgrading a program written in Forth) - but I'm at my wits end here and you guys are always so helpful....

Can somebody please explain how to represent floating point values in hex?  I think I need one sign bit, 8 exponent bits with an offset of 7F and 23 bits for the mantissa.  As a clue, I know that 3F80 0000 is 1.0 and CB41 3333 is 25.4.

Thank You!
Avatar of ozo
ozo
Flag of United States of America image

do you mean 41CB 3333? Your values look like
ANSI/IEEE Standard 754-1985 Standard for Binary Floating Point Arithmetic
http://www.psc.edu/general/software/packages/ieee/ieee.html
Avatar of jjmcd
jjmcd

There are as many ways of representing a float as Carter has little liver pills.  The platform and compiler matter, as well as just how you've defined your float.

In general, most of the floating point representations include a fraction and an exponent.  The fraction is normalized so that the leftmost bit is a 1, and sometimes that 1 is omitted.  The exponent is essentially how many positions to move the binary point (usually NOT the decimal point although sometimes :-)) and is typlcally a 2's complement number with a +/- range that is approximately equal, like +127 to -128, although not always symmetrical.

The IEEE standard ozo pointed you to is a good one,  but on the PC a lot of times floats are stored in the format for the math coprocessor or something easily converted to the math coprocessor format.  The VAX/Alpha has a half-dozen formats supported in hardware, as does the IBM mainframe.  Compilers for these platdorms tend to use the native formats although many also support IEEE if you specify it.

Ain't this stuff fun?

Your 3F80 0000 looks like 1/2 * 2^1, with the binary point to the left of the 80, so 3F must represent moving the binary point 1 bit to the right, although this would imply a Motorola byte sex and the sign bit for the mantissa on the left of the exponent, with a 6 bit exponent - is this a Mac?
Avatar of fisham29

ASKER

The program runs on a Motorola 68HC11 chip.   Since the Forth language has no native floating point support, all the float routines I'm dealing with were written by the author of the application.  I have reason to believe he followed the IEEE standard (although I don't know for SURE yet, until I understand this stuff).
ASKER CERTIFIED SOLUTION
Avatar of sumant032199
sumant032199
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To be honest, this answer wouldn't have helped me too much...I'm accepting it as the answer mainly because I got a program from another source which will take a float input and just TELL me what the HEX value is.   It also is a little unsatisfying, because I didn't learn to do it 'by hand', but it got the job done.   The program is in basic - if anybody wants it, I'll email it to you.
Thanks.
Please mail it to me at
hotsumu@hotmail.com