Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to use bit fields with union?

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;
} flags;

Now i wondered to see if this can be done with a union so i modified the code like,

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

I found the bit field with union works but all those fields in the union are given to a single bit as I understood from output. Now I am seeing it is not erroneous to use bit fields with union, but it seems to me that using it like this is not operationally correct. So what is the answer - is it valid to use bit field with union?

like image 912
amin__ Avatar asked Jul 04 '12 09:07

amin__


People also ask

Can we use bit fields in union?

Bit fields CANNOT be used in union.

Why are bit fields usually implemented as fields within unions?

Bit-fields and unions may be combined in a way that permits programmers to pack and unpack bits in an integer. Unions allow programmers to circumvent C++'s strong typing rules while bit-fields allow programmers to access the bits that encode the compressed information.

Is array of bit fields allowed?

Arrays of bit fields, pointers to bit fields, and functions returning bit fields are not allowed. The optional declarator names the bit field. Bit fields can only be declared as part of a structure. The address-of operator (&) cannot be applied to bit-field components.

Can bit fields used in data structure?

In programming terminology, a bit field is a data structure that allows the programmer to allocate memory to structures and unions in bits in order to utilize computer memory in an efficient manner.


2 Answers

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;
    };
};
like image 109
SpacedMonkey Avatar answered Sep 20 '22 11:09

SpacedMonkey


You are given a gun and bullets. Is it okay to shoot your self in foot with it? Of course not, but nobody can stop you from doing this if you want to.

My point is, just like gun and bullets, union and bit fields are tools and they have their purpose, uses and "abuses". So using bitfields in union, as you have written above, is perfectly valid C but a useless piece of code. All the fields inside union share same memory so all the bitfields you mention are essentially same flag as they share same memory.

like image 31
binW Avatar answered Sep 19 '22 11:09

binW