Connecting Tech Pros Worldwide Forums | Help | Site Map

bit fields in a structure

Neo
Guest
 
Posts: n/a
#1: Nov 14 '05
typedef union
{
struct
{
UINT16 reserved : 14;
UINT16 bank_no : 2;
} bit;
UINT16 word;
} HSL_BANK_SELECT_T;

HSL_BANK_SELECT_T a;
a.bit.bank_no = 1

Is bank_no MSB or LSB?
What is the significance of UINT16 or any other type here?
Can I replace it with UINT8 or UINT32.... arbitrarily?

typedef union
{
struct
{
UINT16 reserved : 14;
UINT16 bank_no : 2;
UINT8 some_flag1 : 1;
UINT8 some_flag2: 1;
} bit;
UINT32 word;
} HSL_BANK_SELECT_T2;

Is this valid to have UINT16 and UINT8 intermixed...???

-Neo



Eric Sosman
Guest
 
Posts: n/a
#2: Nov 14 '05

re: bit fields in a structure


Neo wrote:[color=blue]
>
> typedef union
> {
> struct
> {
> UINT16 reserved : 14;
> UINT16 bank_no : 2;
> } bit;
> UINT16 word;
> } HSL_BANK_SELECT_T;
>
> HSL_BANK_SELECT_T a;
> a.bit.bank_no = 1
>
> Is bank_no MSB or LSB?[/color]

Either. Or neither. The compiler has wide discretion
in the way it arranges bit-fields, and different compilers
do it differently.
[color=blue]
> What is the significance of UINT16 or any other type here?[/color]

The legitimate types for bit-fields are signed and
unsigned `int' and (in C99) _Bool. A compiler may accept
other types as well, but is not required to do so -- which
means that the next compiler you use might not accept these
extensions.

Since you haven't shown how UINT16 is defined, it's
not possible to say whether it's a legitimate bit-field
type. You may be in trouble already.
[color=blue]
> Can I replace it with UINT8 or UINT32.... arbitrarily?[/color]

See above.
[color=blue]
> typedef union
> {
> struct
> {
> UINT16 reserved : 14;
> UINT16 bank_no : 2;
> UINT8 some_flag1 : 1;
> UINT8 some_flag2: 1;
> } bit;
> UINT32 word;
> } HSL_BANK_SELECT_T2;
>
> Is this valid to have UINT16 and UINT8 intermixed...???[/color]

See above.

You have fallen into a trap that has caught many before
you, namely, of thinking that `struct's, with or without
bit-fields, are a means of mapping an externally-imposed
format. Such use may be appropriate for a particular compiler
on a particular machine, but the language definition does not
guarantee that the mapping will be as you hope.

--
Eric.Sosman@sun.com
Neo
Guest
 
Posts: n/a
#3: Nov 14 '05

re: bit fields in a structure



"Eric Sosman" <Eric.Sosman@sun.com> wrote in message
news:408530D6.DFB68D9E@sun.com...[color=blue]
> Neo wrote:[color=green]
> >
> > typedef union
> > {
> > struct
> > {
> > UINT16 reserved : 14;
> > UINT16 bank_no : 2;
> > } bit;
> > UINT16 word;
> > } HSL_BANK_SELECT_T;
> >
> > HSL_BANK_SELECT_T a;
> > a.bit.bank_no = 1
> >
> > Is bank_no MSB or LSB?[/color]
>
> Either. Or neither. The compiler has wide discretion
> in the way it arranges bit-fields, and different compilers
> do it differently.
>[color=green]
> > What is the significance of UINT16 or any other type here?[/color]
>
> The legitimate types for bit-fields are signed and
> unsigned `int' and (in C99) _Bool. A compiler may accept
> other types as well, but is not required to do so -- which
> means that the next compiler you use might not accept these
> extensions.[/color]

What is _Bool ???
[color=blue]
>
> Since you haven't shown how UINT16 is defined, it's
> not possible to say whether it's a legitimate bit-field
> type. You may be in trouble already.[/color]

Here is how I define UINT8, 16 and 32 (for unsigned 8, 16 and 32 bit
quantities)...
typedef unsigned char UINT8;
typedef unsigned short UINT16;
typedef unsigned int UINT32;
[color=blue]
>[color=green]
> > Can I replace it with UINT8 or UINT32.... arbitrarily?[/color]
>
> See above.
>[color=green]
> > typedef union
> > {
> > struct
> > {
> > UINT16 reserved : 14;
> > UINT16 bank_no : 2;
> > UINT8 some_flag1 : 1;
> > UINT8 some_flag2: 1;
> > } bit;
> > UINT32 word;
> > } HSL_BANK_SELECT_T2;
> >
> > Is this valid to have UINT16 and UINT8 intermixed...???[/color]
>
> See above.
>
> You have fallen into a trap that has caught many before
> you, namely, of thinking that `struct's, with or without
> bit-fields, are a means of mapping an externally-imposed
> format. Such use may be appropriate for a particular compiler
> on a particular machine, but the language definition does not
> guarantee that the mapping will be as you hope.
>
> --
> Eric.Sosman@sun.com[/color]


Closed Thread