This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: union issue


Mihai "Don?u" <mihai.dontu@gmail.com> writes:

> On Wednesday 30 May 2007 13:00, Andrew Haley wrote:
>> Mihai Don?u writes:
>>  > typedef struct _type2_t {
>>  >     union {
>>  >         uint32_t field1:31;
>>  >         uint32_t field2:16;
>>  >     };
>>  >     uint32_t field3:1;
>>  > } __PACKED type2_t;
>> 
>> I've read this three times now and I still don't understand your
>> question.  You declare a packed struct, but you don't want it to be
>> packed?  So don't declare it packed, then.  But that can't be what you
>> really meant.
>> 
>> Andrew.
>> 
>
> Ok, I'm sorry if I wasn't clear.
>
> Let me begin with the problem that I'm facing: I have an array of dword-s
> where each dword has the following "bit-fields":
>   * ordinal_or_name (bit 31, i.e. 0x80000000 )
>   * ordinal (bits 0-15, i.e. 0x0000ffff)
>   * name_rva (bits 0-30, i.e. 0x7fffffff)

The following might work for you, though be warned that C bitfields aren't
very suitable to represent data formats. For example, the code won't be
portable between big- and little-endian CPUs.

typedef struct ordinal {
  uint32_t ord:16;
  uint32_t reserved:15;
};

typedef struct name_rva {
  uint32_t name:31;
  uint32_t reserved:1:
};

typedef struct flag {
  uint32_t reserved:31;
  uint32_t flag:1;
};

typedef union fields {
  flag f;
  ordinal o;
  name_rva n;
};

-- Sergei.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]