Unable to save a float value to a bitfield structure

后端 未结 5 1812
时光说笑
时光说笑 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).

提交回复
热议问题