Discussion:
what you can do with bitfields
Add Reply
fir
2024-11-28 13:39:43 UTC
Reply
Permalink
i use them but i never fully chcked what i can do with them
can i say define a structure that is 8 bit size define two
bitfields say high and low for high 4 bits and low four bits then do
somethiong liek that

T a,b; //thuis struct mentioned
a.high = b.low;
a.low = b.high;

?
Lawrence D'Oliveiro
2024-11-29 02:46:17 UTC
Reply
Permalink
i 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.
fir
2024-11-30 15:50:35 UTC
Reply
Permalink
Post by Lawrence D'Oliveiro
i 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
BGB
2024-11-30 19:34:26 UTC
Reply
Permalink
Post by fir
Post by Lawrence D'Oliveiro
i 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
Most common way to do bit flags:
#define WHATEVER_FLAG1 0x0001
#define WHATEVER_FLAG2 0x0002
...

And:
if(flags&WHATEVER_FLAG1)
{ ... }


Though, for larger bit-flags, on some targets it may be more efficient
to do:
if((flags>>WHATEVER_FLAG43_SHR)&1)
{ ... }

For example, RISC-V, where shift-right and mask will use fewer
instructions than constant-load and mask.

...
fir
2024-11-30 22:11:09 UTC
Reply
Permalink
Post by fir
Post by Lawrence D'Oliveiro
i 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)

Loading...