Re: union issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Mihai "Don?u" <mihai.dontu@xxxxxxxxx> 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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux