Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"Flag" variables: Bit fields vs boolean set/test logic, performance implications

61 views
Skip to first unread message

Rashid

unread,
Oct 5, 2009, 4:04:15 PM10/5/09
to
Folx,

was wondering if you could comment on the following.

One can code these and respective routines (set/reset and test) in a
number of ways. A good overview is here: http://en.wikipedia.org/wiki/Bit_field

Has anyone done any performance test on amd64 type system with a
recent enough of a gcc compiler to compare the boolean bit magic vs
bit fields in terms of performance and memory efficiency ?

Say one has a need for 32 flags. These can be packed in a single
unsigned int through traditional boolean logic. What if one defines
them as 32 bit fields - would they still occupy same 4 bytes and will
access to the bit fields be as efficient ?
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

jyoti

unread,
Oct 6, 2009, 2:03:49 AM10/6/09
to
On Tue, 06 Oct 2009 01:34:15 +0530, Rashid <rashid....@gmail.com>
wrote:

> One can code these and respective routines (set/reset and test) in a
> number of ways. A good overview is here:
> http://en.wikipedia.org/wiki/Bit_field
>
> Has anyone done any performance test on amd64 type system with a
> recent enough of a gcc compiler to compare the boolean bit magic vs
> bit fields in terms of performance and memory efficiency ?

I believe there would be no performance difference when the
compiler makes use of hardware based routines. That aside, if
you are looking for the answer for an academic purpose I can
understand but I would not bother for performance issues between
these two if the concern is my application code. I do not think
they would bottleneck performance in general.

> Say one has a need for 32 flags. These can be packed in a single
> unsigned int through traditional boolean logic. What if one defines
> them as 32 bit fields - would they still occupy same 4 bytes and will
> access to the bit fields be as efficient ?

Yes, if unsigned int is 32 bits it will still take 32 bits only. Personally
I would not try to think about the efficiency here in my code, I would use
that
which is easier to read and maintain.

Regards,
Jyoti

0 new messages