Thursday, 19 September 2013

Unable to save a float value to a bitfield structure

Unable to save a float value to a bitfield structure

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;
} my_struct;
my_struct struct1[10];
for(int i=0;i<10; i++)
{
// left some portion
struct1[i].var5= x;// where x is a float value retrieved from a
database with sqlapi++ asDouble()
cout<<"Value of x from db is:\t"<<x; // prints 0.1 if it is stored,
prints 2.4 if 2.4 is fed
cout<<"Value of x stored in struct1 is:\t"<<struct1[i].var5; //
prints 0 instead of 0.1, prints 2 instead of 2.4
}
I want to store floating point values like 0.1, 3.4, 0.8 in var5. But i am
unable to do so. Can somebody help me how could i fix this problem?

No comments:

Post a Comment