bit fields in a structure

E

Eric Sosman

Neo said:
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?

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

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.
Can I replace it with UINT8 or UINT32.... arbitrarily?

See above.
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...???

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.
 
N

Neo

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
 
N

Neo

Eric Sosman said:
Either. Or neither. The compiler has wide discretion
in the way it arranges bit-fields, and different compilers
do it differently.


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.

What is _Bool ???
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.

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top