Re: C question



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
.



Relevant Pages

  • Re: Rephrase - Could you sod off Spinoza
    ... Here p is the assertion that "C is a programming language" and q is ... recompiled using new compilers, and the old compilers are retained to ... best-selling and highly useful books of one Herbert Schildt... ... Evidence has been pointed at you that Herbert Schildt write inaccurate ...
    (comp.programming)
  • Re: Cant understand this! (Help required please)
    ... Yes and some online courses NETG does not seem to understand what ... > New to programming: ... I have heard of comeau don't they make compilers too? ... An argument I have used many times myself, I want to learn standard C++, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Had Phyton suggested
    ... >> programming choppers and dust clouds in scenes. ... >> up and work as simple as for instance the first Pascal compilers. ... > Python is an interpreted language, ... > that you look at java. ...
    (comp.lang.python)
  • Re: Dynamically loading a static C library (Updated; also compiler suggestion?)
    ... I am programming a microcontroller, ... Manufacturer suggests using one of the following compilers: ... or I could use a better development environment for 16- ...
    (comp.arch.embedded)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... prejudices and the resentment of a literate style in programming, ... campaign on some assertions and a Web page created by Programmer Dude ... they were proven on multiple operating ... systems, compilers and CPU architectures. ...
    (comp.programming)