Is it valid to use bit fields with union?

前端 未结 2 1384
别那么骄傲
别那么骄傲 2020-12-17 15:33

I have used bit field with a structure like this,

struct
{
       unsigned int is_static: 1;
       unsigned int is_extern: 1;
       unsigned int is_auto: 1         


        
2条回答
  •  长情又很酷
    2020-12-17 16:09

    It is valid but as you found out, not useful the way you have done it there.

    You might do something like this so you can reset all the bits at the same time using flags.

    union {
        struct {
            unsigned int is_static: 1;
            unsigned int is_extern: 1;
            unsigned int is_auto: 1;
        };
        unsigned int flags;
    };
    

    Or you might do something like this:

    union {
        struct {
            unsigned int is_static: 1;
            unsigned int is_extern: 1;
            unsigned int is_auto: 1;
        };
        struct {
            unsigned int is_ready: 1;
            unsigned int is_done: 1;
            unsigned int is_waiting: 1;
        };
    };
    

提交回复
热议问题