Re: Bit shifting vs Bitfields
Roberto Waltman <usenet@rwaltma n.net> writes:[color=blue]
> richard_l@latte r.demon.co.uk wrote:
> My main query was if there were any benefits to[color=green]
>>bit shifting rather than defining bit fields. I think my conclusion is
>>that unless you are porting code, defining bitfields are fine. I
>>would suggest that most ANSI compilers these days (I'm using gcc)
>>handle bitfields in the appropriate manner.[/color]
>
> I agree with the "appropriat e handling", but not with "bit fields
> unless porting".
>
> I would use bit fields only if all of the following three assumptions
> are true:
>
> (a) It does not matter what bits are assigned to which field. [1]
> (b) The size of the structure containing the bit fields is not
> important. [2]
> (c) The alignment of a bit field structure is not important. [3][/color]
*or*
The compiler precisely documents how it lays out bit fields, and I
know I'll never need to worry about portability to any other compiler
that doesn't make the same guarantees. Yes, masks and shifts will do
the same job, but bit fields are easier to work with.
(A good argument can be made that masks and shifts are a better idea
even in these circumstances.)
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Roberto Waltman <usenet@rwaltma n.net> writes:[color=blue]
> richard_l@latte r.demon.co.uk wrote:
> My main query was if there were any benefits to[color=green]
>>bit shifting rather than defining bit fields. I think my conclusion is
>>that unless you are porting code, defining bitfields are fine. I
>>would suggest that most ANSI compilers these days (I'm using gcc)
>>handle bitfields in the appropriate manner.[/color]
>
> I agree with the "appropriat e handling", but not with "bit fields
> unless porting".
>
> I would use bit fields only if all of the following three assumptions
> are true:
>
> (a) It does not matter what bits are assigned to which field. [1]
> (b) The size of the structure containing the bit fields is not
> important. [2]
> (c) The alignment of a bit field structure is not important. [3][/color]
*or*
The compiler precisely documents how it lays out bit fields, and I
know I'll never need to worry about portability to any other compiler
that doesn't make the same guarantees. Yes, masks and shifts will do
the same job, but bit fields are easier to work with.
(A good argument can be made that masks and shifts are a better idea
even in these circumstances.)
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Comment