Post by firPost by Lawrence D'Oliveiroi use them but i never fully chcked what i can do with them ...
One problem with C is that bitfields can only have static offsets and
sizes. If these were dynamic, you could express much more powerful
manipulations more conveniently than with shifting and masking.
what do you mean?
i must check hovever how this static work at all.. i used them
and not so rarely but only simple cases usually for solo bit flags
#define WHATEVER_FLAG1 0x0001
#define WHATEVER_FLAG2 0x0002
...
if(flags&WHATEVER_FLAG1)
{ ... }
Though, for larger bit-flags, on some targets it may be more efficient
if((flags>>WHATEVER_FLAG43_SHR)&1)
{ ... }
For example, RISC-V, where shift-right and mask will use fewer
instructions than constant-load and mask.
...
and you thing doin it with bitfields would be worse?
i dont tested such things but i use
union Color
{
unsigned u;
struct { unsigned char b,g,r,a;};
};
in my per-pixel graphics codes to spare unpacking/packing unsigned
color into r g b a with shifts and ands or ors
anbd in this case its not only much simpler/shortes yet use this
union is more effective (i tested it bac then some hours and
not met a case when this union woudl be slower than shifts by hand)
(speakin on gcc /tdm for windows)