• C

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!
fisham29Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
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
0
jjmcdCommented:
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?
0
fisham29Author Commented:
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).
0
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

sumant032199Commented:
ozo has given this web site,
http://www.psc.edu/general/software/packages/ieee/ieee.html
and it explains very well about single precision numbers and
double precision numbers.

float or single precision numbers are interpreted as explained in
the site. It won't be a fair idea to convert float into HEX.
There are three parts in float numbers as you know; even if EXPONENET
and SIGNIFICAND are expressed in terms of HEX that would be meaningless.
Ok, We can represent integer part of final single precision number
in HEX format. But the bits on the right of binary point(just like
decimal point) represent 1/2, 1/4, 1/8,...... hence they can't be
converted in to HEX format. I give you a C program using which you
will certainly understand how float are stored in PC. If you still
want to convert the float into HEX then give me detail explanation
of which bit sequence you want to convert into HEX because that is
the only thing can be done with HEX. I am to help you as far as possible.

Now, something about the program. This program has a statement which
generates a warning "Suspicious pointer conversion" let it be as it
is. This will demonstrate the power of C long int pointers to change
value of float numbers. Following program will show how to "make"
6.5 float number using pointers and bitwise programming.


#include <stdio.h>
#include <conio.h>

/*
   GET_BIT returns the contents of bit(1 or 0) represented by BitNo of
   the number represented by No.
   SET_BIT sets the bit to 1.
   CLS_BIT sets the bit to 0.
   OPP_BIT sets the bit to complement of current bit.
*/
#define GET_BIT(No,BitNo) ((No >> BitNo) & 1)
#define SET_BIT(No,BitNo) (No |= (1L << BitNo))
#define CLS_BIT(No,BitNo) (No &= ~(1L << BitNo))
#define OPP_BIT(No,BitNo) (GET_BIT(No,BitNo)? CLS_BIT(No,BitNo) : SET_BIT(No,BitNo))

int main(void)
{
      float n=0.000E+2,*ptr;
      long int *p;
      char c;
      clrscr();
      ptr=&n; p=ptr;

      //*p |= 0x80000000L;  // 31th bit=1.

      SET_BIT(*p,30);
      SET_BIT(*p,23);


      SET_BIT(*p,22);
      SET_BIT(*p,20);



      //CLS_BIT(*p,31);
      printf("Number = %f\n31---30-----------23----22------------------------------------------0\n",n,*p,p);
      for(c=31;c>=0;c--)
       {
         if(c==30) printf("   ");
         if(c==22) printf("   ");
         printf("%d ",GET_BIT(*p,c));
       }
      getch();
      getch();
      return 0;
}
/*  Left-Right shifting not allowed in floating point numbers.

31      30...........23       22..................................0
sign       Exponent                         Fraction

1 Bit       8 Bits                          23 Bits
*/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fisham29Author Commented:
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.
0
sumant032199Commented:
Thanks.
Please mail it to me at
hotsumu@hotmail.com
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.