Bit fields Vs logical operators?

R

Rui Maciel

While writing a small Base64 converter I was faced with the task of performing a fair share of bit twiddling. I've noticed that through the use of a couple of structs and unions I could let go of a hand full of logical operations. The question is, is it worth it? Does it have any impact in the code's performance or does it end up being nothing more than syntactic sugar?


Thanks in advance,
Rui Maciel
 
B

Ben Pfaff

Rui Maciel said:
While writing a small Base64 converter I was faced with the task of
performing a fair share of bit twiddling. I've noticed that through
the use of a couple of structs and unions I could let go of a hand
full of logical operations. The question is, is it worth it? Does it
have any impact in the code's performance or does it end up being
nothing more than syntactic sugar?

If you are considering bit fields for extracting data from
structures that are transmitted from one machine to another, then
I would discourage it. The ordering of bits within units, and
the size of each unit, is implementation-defined, so even if your
code works well on one implementation it will likely fail on
another.

If you are considering bit fields for converting data from one
format to another via union members ("type punning"), that's not
a good idea either, both for the same reason as before and
because it is formally undefined.
 
R

Rui Maciel

Ben said:
If you are considering bit fields for extracting data from
structures that are transmitted from one machine to another, then
I would discourage it. The ordering of bits within units, and
the size of each unit, is implementation-defined, so even if your
code works well on one implementation it will likely fail on
another.

If you are considering bit fields for converting data from one
format to another via union members ("type punning"), that's not
a good idea either, both for the same reason as before and
because it is formally undefined.

Thanks for the help, Ben. Kudos!


Rui Maciel
 
M

Marcus Harnisch

Hi Ben

Ben Pfaff said:
The ordering of bits within units, and the size of each unit, is
implementation-defined, so even if your code works well on one
implementation it will likely fail on another.

Partially true. Whether or not using bit-fields makes sense also
depends on requirements regarding portability. Implementation of
bit-fields is often regulated by a target architecture's ABI.

If multi-architecture portability is the issue, then using bit-fields
might not be ideal. If a limited set of architectures has to be
supported (perhaps only one), then you could define the bit-field
structures for each one.

Regards
Marcus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top