Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Re: Bit-fields are brain-dead (was Looking to buy compiler)

2005-11-10 by brendanmurphy37

Tom,

I think you hit the nail on the head on both the "pro" and "anti" case 
for using bit-fields for accessing hardware, with your statement:

> 
> Now, I would prefer to let the compiler figure out how it is going to 
> access the bit fields within those long words instead of me ANDing 
and 
> SHIFTing to extract data...  No?  Isn't the code more readable with 
> "*theTime->tm_min = ctime0.minutes;*" instead of "*theTime->tm_min = 
> (ctime0 & 0xf0) >> 4;*" ???
> 

Pro bit-field: "the code is more readable"
Anti bit-field: "let the compiler figure it out"

The point on the "anti" side is, the compiler WILL figure it out, in 
any way it chooses. Yes, I've no doubt the code sample you gave works, 
but will it work with a different compiler? a new revision of the same 
compiler? a new set of optimisation flags? I don't know, and I can't 
see how anyone would. There's just too much flexibility available for 
the compiler to generate whatever it wants, whilst still conforming to 
ANSI 'C', to guarantee it will place stuff where you want it, and with 
the access restrictions required (always using 32-bit access for h/w 
registers for example).

I remain to be convinced: the only argument I've seen on the "pro" side 
is your one on readability. However, if you're that upset about "ctime0 
& 0xf0) >> 4;" (and I can see why!), the pre-processor can be used. 
It's not ideal, but it can certainly get you closer to where we'd all 
want to be (easy to read code that is guaranteed to always work).

I just don't see the effort and risks involved in using bit-fields as 
you suggest is worth it. The effort involved in verifying the compiler 
generates exactly what you want, every time you change a compiler flag, 
or version, or compiler, just isn't worth it. Of course, you could 
always skip the verification stage, cross your fingers and hope nothing 
bad happens.....

By the way, I think readability is extremely important: in other cases, 
it is way more important to use language features appropriately to 
clarify intent. Just that in this case, it is just asking for trouble.

I remain to be convinced otherwise!

Brendan

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.