The program below seems to work for me. I get:
sign exponent mantissa
1 81 59999a
1 401 b333333333333
The two changes I made are using the proper C99 format PRIx64
in printf()
(See this wikipedia page for information about these formats) and using the same base type for the bit-fields in the struct in union FloatingPointDoublePrecisionIEEE754
. Bit-fields are notoriously under-specified but using different base types may cause the compiler to start a new word, not what you want here.
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
/*
* See also : http://class.ece.iastate.edu/arun/CprE281_F05/ieee754/ie5.html
*/
union FloatingPointSinglePrecisionIEEE754 {
struct {
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} raw;
float f;
} fnumber;
union FloatingPointDoublePrecisionIEEE754 {
struct {
uint64_t mantissa : 52;
uint64_t exponent : 11;
uint64_t sign : 1;
} raw;
double d;
} dnumber;
int main() {
printf("sign\texponent\tmantissa\n");
fnumber.f = -6.8;
printf("%x\t%x\t\t%x\n",
fnumber.raw.sign,
fnumber.raw.exponent,
fnumber.raw.mantissa);
dnumber.d = -6.8;
printf("%x\t%x\t\t%" PRIx64 "\n",
dnumber.raw.sign,
dnumber.raw.exponent,
(uint64_t)dnumber.raw.mantissa);
return 0;
}