/** * float - print out the parts of a floating point number. */ #include #include int f2i (float f) { return *((int *) &f); } // f2i void show_int_bits (int i) { int j; int current; // For each bit position for (j = 31; j >= 0; j--) { // Check what the bit at position j is // Need the number corresponding to 00000100000000000, that is, // 1 followed by j 0's // Can do it with expontentiation, but that's wasteful current = 0x1 << j; if ((i & current) == 0) printf ("0"); else printf ("1"); } printf ("\n"); } // show_int_bits int main (int argc, char **argv) { float f; // The float we wish to deconstruct int i; // The digits // Verify parameters if (argc != 2) { printf ("Usage: float NUM\n"); return EXIT_FAILURE; } // Get the number f = atof (argv[1]); // Convert to bits i = f2i (f); // Strip out anything but the exponent. i = i >> 23; i &= 255; // Handle special cases if (i == 0) printf ("%f is a special case (exponent 00000000)\n", f); else if (i == 255) printf ("%f is a special case (exponent 11111111)\n", f); else printf ("For float %f, the exponent is %d\n", f, i-127); return EXIT_SUCCESS; } // main