Home > Enterprise >  Float to binary conversion in C(ieee 754)
Float to binary conversion in C(ieee 754)

Time:11-07

I am trying to get the 0000's to left of the conversion but I have a bit of trouble.

For example if I ask for float_number:5

--> I get 101

--> but Correct Answer is 01000000101000000000000000000000

#include <stdio.h>

    union intFloat
{
    int i;
    float f;
};


unsigned show_bits(unsigned input)
{
    int checker = input;
    int i,counter;

    int size = (sizeof(input)*8)-1;
    char *buffer = (char*)malloc(size 2);
    for(i=size,counter=0; i>=0; i--,counter  ) 
    {
        
        if ( checker == 0)
            return 0;

        
        if ( checker == 1 )
            return 1;

        return (input % 2)   10 * show_bits(input / 2);
    }
}

int main() 
{
union intFloat data;

    printf("Enter an int for encoding: ");
    scanf("%d", &data.i);
    printf(" Number = %d ",show_bits(data.i));
    printf("\n");

    printf("Enter a float for encoding: ");
    scanf("%f", &data.f);
    //call show bits
    printf(" Number = %d ",show_bits(data.f));

    return 0;
}

I've tried to source online answer but every code in C is differently structure. I am a novice in C but would like to get more familiar with the language.

CodePudding user response:

Your implementation of show_bits is a bit bizarre:

  • Why the for? You return immediately.
  • Why malloc and then immediately leak?
  • Why is your implementation recursively multiplying?
  • You don't have 32 digit precision with an unsigned

I changed the function to be:

#include <limits.h>
void show_bits(unsigned u) {
    const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
    printf("Number: ");
    // Iterate over bits starting at the MSB
    for (unsigned bit = 0; bit < unsigned_bits; bit  ) {
        printf("%u", (u & (1 << (unsigned_bits - bit - 1)) ? 1 : 0));
    }
    printf("\n");
}

Using bit operations directly.

Then, after you scan the float, you mistakenly access the .f member. This is the core step where the punning happens. If you just use .f, you get the float member, and it will be converted to the unsigned int 5 automatically by the language upon being passed to a function taking unsigned.

Instead, you need to be using data.i. Type punning with unions is based on the fact, that the underlying memory is the same. The float is written into the memory of data. Now, when we access that memory through data.i, we treat the float's bit presentation as if it were an (unsigned) int. Therefore:

int main() {
    union {
        int i;
        float f;
    } data;

    printf("Enter an int for encoding: ");
    scanf("%d", &data.i);
    show_bits(data.i);

    printf("Enter a float for encoding: ");
    scanf("%f", &data.f);
    show_bits(data.i);
}

Now correctly outputs:

Enter an int for encoding: 5
Number: 00000000000000000000000000000101
Enter a float for encoding: 5
Number: 01000000101000000000000000000000

CodePudding user response:

Yuo need to access its binary representation.

int printfloatasbin(const float x)
{
    unsigned long long mask = 1ull << (sizeof(x) * CHAR_BIT - 1);
    unsigned long long val;

    memcpy(&val, &x, sizeof(x));
    while(mask)
    {
        printf("%d",!!(val & mask));
        mask >>= 1;
    }
}

int main(void)
{
    printfloatasbin(5.0f);
}
  • Related