/* file: float_to_ieee.c Displays the internal representation of a single precision floating point number on MIPS RISC R2000, R3000, and R4000 series processors Available via anonymous ftp on sgi.sgi.com in the directory ~ftp/support/Pipeline To compile: % cc float_to_ieee.c -o float_to_ieee Sample run: % ./float_to_ieee Enter decimal floating point number: -3.5 C0 60 00 00 (hex) 11000000 01100000 00000000 00000000 (binary) */ main() { /* loop counter */ int ii; /* mask to print out binary representation */ unsigned int bit_mask = 0x80000000; /* structure for printing the hex /* representation */ struct byte_representation { unsigned int byte1: 8; unsigned int byte2: 8; unsigned int byte3: 8; unsigned int byte4: 8; } *byte_number; /* union maps the floating point */ /* number as a */ union fp_representation { /* float, */ float float_rep; /* int, and byte */ unsigned int int_rep; } my_number; byte_number = (struct byte_representation *) \ &my_number.float_rep; printf ("\nEnter decimal floating point "); printf ("number: "); scanf ("%f", &my_number.float_rep); /* PRINT NUMBER IN DECIMAL */ printf ("\n%f = \n", my_number.float_rep); /* PRINT NUMBER IN HEX */ printf (" "); if (byte_number->byte1 < 0xa) { /* print leading zero */ printf ("0"); printf ("%x ", byte_number->byte1); } else { printf ("%x ", byte_number->byte1); } if (byte_number->byte2 < 0xa) { /* print leading zero */ printf ("0"); printf ("%x ", byte_number->byte2); } else { printf ("%x ", byte_number->byte2); } if (byte_number->byte3 < 0xa) { /* print leading zero */ printf ("0"); printf ("%x ", byte_number->byte3); } else { printf ("%x ", byte_number->byte3); } if (byte_number->byte4 < 0xa) { /* print leading zero */ printf ("0"); printf ("%x ", byte_number->byte4); } else { printf ("%x ", byte_number->byte4); } printf ("(hex) \n"); /* PRINT NUMBER IN BINARY */ printf (" "); for (ii=0;ii<32;ii++) { if ((ii%8) == 0) printf (" "); if ((my_number.int_rep & bit_mask) == 0) printf ("0"); else printf ("1"); bit_mask = bit_mask >> 1; } printf (" (binary) \n\n"); }