What's the correct way of using bitfields in C?

孤街浪徒 提交于 2019-12-04 04:46:55

You might be missing a struct inside your union.

typedef struct
{
    union{
       struct {
           unsigned long mantissa: 23;
           unsigned long exponent: 8;
           unsigned long sign: 1;
       } float_parts;
       float all;
    };
}_float __attribute__((__packed__));

Note that the order of mantissa/exponent and sign depends one the endianess of the cpu.

The problem is that it is a union. It should be 'struct'.

If you are on a glibc platform you can take a look on the ieee754.h header file. It takes care about the endianess stuff. If not it's still probably worth to take a look on it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!