Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.

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!

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!

ANSI/IEEE Standard 754-1985 Standard for Binary Floating Point Arithmetic

http://www.psc.edu/general/software/packages/ieee/ieee.html

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?

All Courses

From novice to tech pro — start learning today.

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--

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........................

sign Exponent Fraction

1 Bit 8 Bits 23 Bits

*/