Deutsch   English   Français   Italiano  
<94eeb2627d1afdb164e0ba8c74ddd8a65affbdf3@i2pn2.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!i2pn.org!i2pn2.org!.POSTED!not-for-mail
From: fir <profesor.fir@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: what you can do with bitfields
Date: Sat, 30 Nov 2024 23:11:09 +0100
Organization: i2pn2 (i2pn.org)
Message-ID: <94eeb2627d1afdb164e0ba8c74ddd8a65affbdf3@i2pn2.org>
References: <a5a595ec2b0139d6976cea0e633fc911e38f9f58@i2pn2.org>
 <vib9tp$q0vu$2@dont-email.me>
 <61b392a7f6f2e8974c7bf026e904c63c29ab114a@i2pn2.org>
 <vifpcd$1tgmv$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 30 Nov 2024 22:11:11 -0000 (UTC)
Injection-Info: i2pn2.org;
	logging-data="675797"; mail-complaints-to="usenet@i2pn2.org";
	posting-account="+ydHcGjgSeBt3Wz3WTfKefUptpAWaXduqfw5xdfsuS0";
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:91.0) Gecko/20100101
 Firefox/91.0 SeaMonkey/2.53.19
In-Reply-To: <vifpcd$1tgmv$1@dont-email.me>
X-Spam-Checker-Version: SpamAssassin 4.0.0
Bytes: 2697
Lines: 51

BGB pisze:
> 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.
> 
> ...
> 
> 
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)