[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [avr-gcc-list] Bit-wise structure and unions
From: |
Eric Weddington |
Subject: |
RE: [avr-gcc-list] Bit-wise structure and unions |
Date: |
Tue, 21 Nov 2006 13:31:13 -0700 |
Hi Keith
[Long time, no hear. ;-)]
Comments below.
> -----Original Message-----
> From:
> address@hidden
> [mailto:address@hidden
> org] On Behalf Of Keith Gudger
> Sent: Tuesday, November 21, 2006 11:41 AM
> To: address@hidden
> Subject: [avr-gcc-list] Bit-wise structure and unions
>
> Below is a program which illustrates the following issue:
>
<snip>
> But I would rather not have the union. Is there any other way around
> this? Thanks.
>
Unfortunately, no. The union would be the standard solution.
Ok, I take that back but only slightly. Why use bit fields at all? They are
inherently non-portable between compilers (if they work, then you're lucky).
Inherently, bit fields are tied to an int, which on the avr port is a 16-bit
type. We're lucky in that you can get GCC to put them into an unsigned char.
But again, that's compiler dependent.
You should really use the C language bitwise operators (|, &, ~, ^, <<, >>)
and use bit masks to manipulate the bits that you want.
The bit-wise operators have one advantage over using the bit-field
structure. Using bit-wise operators you can operate on multiple,
*non-contiguous* bits simultaneously within a variable. You can't do that
with bit-field structures.
Eric Weddington
(Give me a call if you want, and we can discuss it more)