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

Using this as a reference: http://www.daniweb.com/code/snippet217332.html
You could push all the decimals over to the whole side; then cast it over to the integer, giving you the signifigand and the exponent obviously also being known.

I haven't tested that tactic, but you could give it a try.

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.

Ok, thanks. I'll try this tonight.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.