I want to convert double num to binary, but mantissa does not work and I dunno why, here is the code... please do not use other libs (only stdio)
void printBinary(int n, int i)
{
int k;
for (k = i - 1; k >= 0; k--) {
if ((n >> k) & 1)
printf("1");
else
printf("0");
}
}
typedef union {
double f;
struct
{
unsigned long long mantissa : 52;
unsigned long long exponent : 11;
unsigned long long sign : 1;
} raw;
} myDouble;
void printIEEE(myDouble var)
{
printf("%d |", var.raw.sign);
printBinary(var.raw.exponent, 11);
printf(" | ");
printBinary(var.raw.mantissa, 52);
printf("\n");
}
then I do in main
myDouble var;
var.f = -8.34;
printf("IEEE 754 representation of %f is : \n",
var.f);
printIEEE(var);
Your binary print function is
void printBinary(int n, int i)
but you are passing unsigned long long
values. Please change the function to
void printBinary(unsigned long long n, int i)