Bitfield masks in C
atomic, bit-fields, c
Solution
Or, if you really wanted the mask:
union Reference {
unsigned asWord;
struct {
unsigned age : 3;
unsigned marked : 1;
unsigned references : 4;
} asFields;
}
Reference agemask_ref;
agemask_ref.asFields = (typeof(agemask_ref.asFields)){0, -1, -1};
unsigned agemask = agemask_ref.asWord;
Problem
Is there a portable way in C to find out the mask for a bit field at compile time? Ideally, I'd like to be able to atomically clear a field like this: ``` struct Reference { unsigned age : 3; unsigned marked : 1; unsigned references : 4; }; struct Reference myRef; __sync_and_and_fetch(&myRef, age, ~AGE_MASK); ``` Otherwise I have to take out a lock on the struct, which is more heavyweight than I'd like.