0

Hi
I'm trying to extract the exponent and mantissa from a float. It seems that the most forward way would be to read the bits of the number, according to IEEE 754 (http://steve.hollasch.net/cgindex/coding/ieeefloat.html). So I need to do bitwise operations to float variable. According to what I have found on the net, I need to cast the float to a char, whithout changing the actual bits in the variable. How do I do this?

float foo;
char fooChar = (char)foo;

Doesn't seem to do the right thing.

Thanks

3
Contributors
3
Replies
6
Views
6 Years
Discussion Span
Last Post by hadoque
0

Adding to what MosaicFuneral has suggested, you can try this, if this is what you are looking for

float val = 2.0;

// make an unsigned char pointer point to the beginning, then extract all bytes
unsigned char *ptr = (unsigned char *)&val;

// first byte         
unsigned char p = *ptr;
printf("\n%x", p);     // print first byte

So, increment the pointer to extract subsequent bytes, also, be careful about the endianness.

Edited by myk45: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.