Printing a Bit Dump of Floating Point Types
Printing a Bit Dump of Floating Point Types
The following example illustrates a C programming technique that is not necessarily
Macintosh- specific.
// Printing a bit dum of floating point types
// The following example shows a way to do a bit dump
// of the floating point types float and double
#include
union {
float x;
unsigned char c[sizeof(float)];
} fnum;
union {
double x;
unsigned char c[sizeof(double)];
} dnum;
main()
{
int i,j;
fnum.x = 1.008;
dnum.x = fnum.x;
printf("float = %g\n", fnum.x);
for (i = 0; i < 4; i++) {
for (j = 0; j < 8; j++)
printf("%c", (fnum.c[i] & j ? '1' : '0'));
printf("|");
}
printf("\n");
printf("double = %g\n", dnum.x);
for (i = 0; i < 10; i++) {
for (j = 0; j < 8; j++)
printf("%c", (dnum.c[i] & j ? '1' : '0'));
printf("|");
}
printf("\n");
}