I am currently working on a program where I need to have this kind of output:
If you don't want to click on the link, I have to output the binary in IEEE 754 of 64 and 32-bit numbers in C.
I already have the double and single floating point approximation, but I'm having trouble finding out how to output the binary of these in IEEE 754 notation, and color code them as well. Any thoughts/solutions on how to do this would be much appreciated.
Here is my current code: I included stdio.h, stdlib.h, string.h, and stdint.h.
The main error I am experiencing is outputting the binary of 64 and 32-bit integers.
int main(int argc, char* argv[])
{
char sentinel[20] = "";
printf("Enter a number (e to exit):\n");
fgets(sentinel,19, stdin);
float fValue = 0.0;
double dValue = 0.0;
float f = fValue;
uint32_t u;
int i = 31;
while (strcmp(sentinel,"e") != 0)
{
dValue = strtod(sentinel, NULL);
printf("\nDouble-precision floating point operation: \n%.17lf",dValue);
fValue = strtof(sentinel, NULL);
printf("\nSingle-precision floating point operation:\n %.17f", fValue);
printf("\n\n");
f = 3.14;
i = 31;
memcpy(&u, &f, sizeof u);
for (i; i >= 0; i--)
putchar('0' + ((u >> i) & i ));
//printf("\n64-bit IEEE 754 binary representation: \n %i", u);
printf("\n32-bit IEEE 754 binary representation: \n %b", fValue);
printf("\nEnter a number (e to exit):\n");
fgets(sentinel,19, stdin);
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire