Unable to save a float value to a bitfield structure

后端 未结 5 1805
时光说笑
时光说笑 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条回答
  • 2020-12-18 11:49

    var5 is a 4-bit unsigned integer that can hold values 0..15 (working on a reasonable assumption that u32 is a synonym for something like unsigned int).

    You can't store fractional values in it. 0.1 and 0.8 would be stored as 0; 3.4 would be stored as 3.

    If you really want fractions, you're going to have to work out how to represent them (fixed point arithmetic).

    0 讨论(0)
  • 2020-12-18 11:54

    You cannot store a float value in a bit-field structure. Floats have to adhere to a specific standard (IEEE 754) that specifies a representation. These representations are for 32 and 64 bits on x86. Therefore a bit field wouldn't have the necessary space to properly represent a floating point value..

    Bit-fields muse be a signed or unsigned integer.

    0 讨论(0)
  • 2020-12-18 11:55

    You can't store a double (64 bits) into a field of only 4 bits. You can either do it this way:

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

    or this way

    struct {
       u32 var1 :7;
       u32 var2 :4;
       u32 var3 :4;
       u32 var4 :1;
       u64 var5 :64;
       u32 var6 :7;
       u32 var7 :4;
       u32 var8 :1;
            } my_struct; 
      ..
    
      struct1[i].var5 = *(u64*)&x; // reinterpret the double as a memory array of 8 bytes
    

    The second way is not recommended.

    If you want to store 64 bits in 4 bits, read up on how floating point (IEEE) works. http://en.wikipedia.org/wiki/IEEE_floating_point

    0 讨论(0)
  • 2020-12-18 11:57

    You can't. A float value is 32-bits in size you certainly can't store it in the 4-bits var5 has available to it. The other 28 bits are very necessary.

    0 讨论(0)
  • 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 <stdio.h>      /* printf */
    #include <stdlib.h>     /* 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<len;i++)
            {   //store binary digits into an array.
                array[i] = (buf[i]-'0');    
            }
            //Now you have an array of integers, either '1' or '0'
            //you can use this to populate your bit field, but you will
            //need more fields than you currently have.
    
            printf("Enter any key to continue or 'q' to exit.");
            scanf("%s", quit);
        }
        return 0;
    }
    
    const char *byte_to_binary32(long x)
    {
        static char b[33]; // bits plus '\0'
        b[0] = '\0';
        char *p = b;  
    
        unsigned __int64 z;
        for (z = 2147483648; z > 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

    0 讨论(0)
提交回复
热议问题