Sujet : Re: what you can do with bitfields
De : cr88192 (at) *nospam* gmail.com (BGB)
Groupes : comp.lang.cDate : 30. Nov 2024, 20:34:26
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vifpcd$1tgmv$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On 11/30/2024 9:50 AM, fir wrote:
Lawrence D'Oliveiro pisze:
On Thu, 28 Nov 2024 14:39:43 +0100, fir wrote:
>
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.
...