Re: C question



On Wed, 27 Jul 2005 15:34:34 -0000, Grant Edwards <grante@xxxxxxxx>
wrote:

>On 2005-07-27, Colin Paul Gloster <Colin_Paul_Gloster@xxxxxxx> wrote:
>
>> As already answered by someone else, you have found an example
>> of bit fields. The reason no field name exists on line 3 is
>> because it is just padding.
>>
>> Do not use bit fields in C. From page 150 of the second
>> edition of Kernighan and Ritchie, "The C Programming
>> Language", ISBN 0131103628:
>
>If you're program is supposed to be portable, I would agree.
>Much of what one writes in an embedded program has no hope
>whatsoever of being portable, so bitfields have their place.
>
[Snipped]

Ones code should at least be portable between compilers, and between
different versions of the same compiler. It also should not break
when the optimisation level is changed. I have worked with compilers
where with low optimization, bitfields were byte aligned. When upping
the optimization to maximum, the bitfields were suddenly word aligned.
In my opinion bitfields should not be used where the underlying
structure is important. Use masks and shifts instead. If one wants to
do such things as calculations with a 12bit sized integer, then
bitfields are nice because the underlying implimentation does not
matter.

Regards
Anton Erasmus
.



Relevant Pages

  • Re: Intel Atom vs ARM
    ... In fact even most non-C99 compilers can compile this code ... flawlessly - long long was a defacto standard well before C99. ... This AND is normally needed for all stores into a bitfield anyway, to make sure that the compiler isn't clobbering the next field. ... I.e. approximately 2-3 times faster than bitfields for L1 cached data. ...
    (comp.arch)
  • Re: Where are the Opteron F vs. Woodcrest reviews?
    ... compilers job a little better is rarely enough. ... ten instructions to one instruction OoO CPUs are still going to execute the ... You can get this in C thgough manually managing your bitfields. ...
    (comp.arch)
  • Re: Intel Atom vs ARM
    ... In fact even most non-C99 compilers can compile this code ... flawlessly - long long was a defacto standard well before C99. ... regard to size or packing. ... You can do bit packing by hand of course, but using bitfields gives the ...
    (comp.arch)
  • Re: [PATCH][2.6] IBM PowerPC Virtual Ethernet Driver
    ... > Well, to be fair, most compilers still aim to make them useful. ... a lot of compiler people do seem to look to standards more than ... > that gcc has useful semantics for bitfields and hardware accesses. ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Compiler bug
    ... >> And please post a bug ID. ... 6257856 Bad optimization of 32bit-sized bitfields of an unsigned 64bit integer ... Casper ...
    (comp.unix.solaris)