Hi there,
is there any difference between using bit fields structure
and plain variable plus macros for dealing with bits?
Let's say I have a two bytes divided as follow
0 - 2: level
3 - 6: type
7 - 10: whatever
11 - 15: future
The two methods are
=== First Method ===
typedef struct {
uint16_t level: 3;
uint16_t type: 4;
uint16_t whatever: 4;
uint16_t future: 5;
} myvar;
myvar x;
x.level = 2;
etc.. etc..
=== Second Method ===
#define LEVEL(x) ( (x) & 0x0007 )
#define TYPE(x) ( ((x) & (0x00015 << 3)) >> 3)
#define SET_TYPE(x) & ( (x) << 3 )
// ... whatever macro I fancy need ...
uint16_t info;
info |= SET_TYPE(4);
Now beside, macros being corrected or not, is it all about personal taste
or is there some real difference between the two methods? And if there is,
which one is it?
I googled for the comparison but found nothing.
Thanks in advance!
S.