Re: C question
- From: Grant Edwards <grante@xxxxxxxx>
- Date: Wed, 27 Jul 2005 15:34:34 -0000
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.
> "[..]
> Almost everything about fields is implementation-dependent.
So. That's what embedded programming is about.
> Whether a field may overlap a word boundary is
> implementation-defined. [..]
> Fields are assigned left to right on some machines and
> right to left on others. [..]"
>
> Use bitwise operators instead of bit fields if you want to use
> C,
Some compilers generate better code for bitfields than they do
for bitwise operators. If speed and space aren't a concern,
then bitwise operators are indeed more portable. But, for some
compilers I've used, using a bitfield cut the number of
instructions generated for a bit set/clear operation by 2/3.
Ideally, the compiler should generate the same code for either
approach, but some don't.
> if you would like to use notation like bit fields' then
> consider programming in Ada ( news:comp.lang.ada ) and taking
> advantage of its representation clauses (please see the bottom
> of WWW.AdaIC.org/standards/95lrm/html/RM-13-5-1.html for an
> example).
It does seem ironic that the most popular language used in
embedded systems programming doesn't allow the programmer to
specify low-level storage layout.
--
Grant Edwards grante Yow! Hold the MAYO & pass
at the COSMIC AWARENESS...
visi.com
.
- Follow-Ups:
- Re: C question
- From: Anton Erasmus
- Re: C question
- References:
- C question
- From: Xarion
- Re: C question
- From: Colin Paul Gloster
- C question
- Prev by Date: Re: Tiny ARM Development
- Next by Date: Re: Learning embedded coding, which uC?
- Previous by thread: Re: C question
- Next by thread: Re: C question
- Index(es):
Relevant Pages
|