#include #include #include int main() { float f; double d; int *p, *p1; int s, e, mf, res; int md[2]; while (true) { printf("Input a real number:\n"); res = scanf("%lf", &d); if (res <= 0) break; f = (float) d; // Print the float representation // in human-readable form int exponent = 0; char s = '+'; if (d < 0.) s = '-'; double r = fabs(d); while (r < 1. || r >= 2.) { if (r < 1.) { r *= 2.; --exponent; } else { assert(r >= 2.); r /= 2.; ++exponent; } } printf("= %c %f * 2 ^ %d\n\n", s, r, exponent); // Float ------------------------- printf("Float representation:\n"); p = (int*) &f; printf("Hex: %08x\n", *p); s = ((*p >> 31) & 1); e = ((*p >> 23) & 0xff); mf = (*p & 0x7fffff); printf("s=%d, e=%d, m=", s, e); unsigned int mask = 0x400000; while (mask != 0) { if ((mask & mf) != 0) printf("1"); else printf("0"); mask >>= 1; } printf("\n\n"); // Double ------------------------- printf("Double representation:\n"); p = (int*) &d; // Lower word p1 = p + 1; // Upper word printf("Hex: %08x %08x\n", *p1, *p); s = ((*p1 >> 31) & 1); e = ((*p1 >> 20) & 0x7ff); // 11 bits md[0] = (*p1 & 0xfffff); // 20 upper bits of mantissa md[1] = *p; // 32 lower bits of mantissa printf("s=%d, e=%d, m=", s, e); mask = 0x80000; while (mask != 0) { if ((mask & md[0]) != 0) printf("1"); else printf("0"); mask >>= 1; } mask = 0x80000000; while (mask != 0) { if ((mask & md[1]) != 0) printf("1"); else printf("0"); mask >>= 1; } printf("\n\n"); } return 0; }