Unable to save a float value to a bitfield structure

后端 未结 5 1814
时光说笑
时光说笑 2020-12-18 11:16

I have a structure

struct {
   u32 var1 :7;
   u32 var2 :4;
   u32 var3 :4;
   u32 var4 :1;
   u32 var5 :4;
   u32 var6 :7;
   u32 var7 :4;
   u32 var8 :1;
          


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-18 12:06

    You can do what you ask with a few intermediate steps. First convert your float to an int, then convert that int into a binary representation. From there, you can assign the resultant values to your bit field. This answer only addresses the intermediate steps.

    The information here provides background and corroboration that 5.2 float is represented by 01000000101001100110011001100110. Decomposing a float into a binary representation can be done many different ways. This is only one implementation or representation. Reversing this process (i.e. going from binary back to float) would require following the same set of rules laid out in the link, backwards.

    Note: endian is also a factor, I ran this in Windows/Intel environment.

    Here is the code:

    #include       /* printf */
    #include      /* strtol */
    
    const char *byte_to_binary32(long int x);
    const char *byte_to_binary64(__int64 x);
    int floatToInt(float a);
    __int64 doubleToInt(double a);
    
    int main(void)
    {
         long lVal, newInt;
         __int64 longInt;
        int i, len, array[65];
        int len1, len2, len3, len4, len5, len6;
        char buf[100];
        char quit[]={" "};
        float fNum= 5.2;
        double dpNum= 5.2;
        long double ldFloat;
    
        while(quit[0] != 'q')
        {
            printf("\n\nEnter a float number: ");
            scanf("%f", &fNum);
            printf("Enter a double precision number: ");
            scanf("%Lf", &ldFloat);
    
            newInt = floatToInt(fNum);
            {
                //float
                printf("\nfloat: %6.7f\n", fNum);  
                printf("int: %d\n", newInt);  
                printf("Binary: %s\n\n", byte_to_binary32(newInt));
            }
            longInt = doubleToInt(dpNum);
            {
                //double
                printf("double: %6.16Lf\n", ldFloat);  
                printf("int: %lld\n", longInt);  
                printf("Binary: %s\n\n", byte_to_binary64(longInt));  
                /* byte to binary string */
                sprintf(buf,"%s", byte_to_binary64(longInt));
            }
            len = strlen(buf);
            for(i=0;i 0; z >>= 1)       //2^32
        {
            *p++ = (x & z) ? '1' : '0';
        }
        return b;
    }
    const char *byte_to_binary64(__int64 x)
    {
        static char b[65]; // bits plus '\0'
        b[0] = '\0';
        char *p = b;  
    
        unsigned __int64 z;
        for (z = 9223372036854775808; z > 0; z >>= 1)       //2^64
        {
            *p++ = (x & z) ? '1' : '0';
        }
        return b;
    }
    
    int floatToInt(float a)
    {
        return (*((int*)&a));   
    }
    
    __int64 doubleToInt(double a)
    {
        return (*((__int64*)&a));   
    }
    

    Here is an image of the results (updated for 32 and 64bit):

    enter image description here

提交回复
热议问题