public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Bit Field Formations
@ 2005-01-21 15:53 Crescioli, Phil
  2005-01-22  3:50 ` Kevin P. Fleming
  0 siblings, 1 reply; 2+ messages in thread
From: Crescioli, Phil @ 2005-01-21 15:53 UTC (permalink / raw)
  To: gcc-help; +Cc: Crescioli, Phil

Hello all,
I'm using gcc version 3.2.2 (RedHat Linux 3.2.2-5) and am having some
questions with regard to how gcc forms bit-fields.

If I create a structure that includes bit-field definitions, how are the
bit fields assigned within a word. In the example below, is variable x1
assigned to bits 31-28 or bits 3-0? My assumption is that the fields are
assigned in order so that the example produces either:

X1:31-28, x2:27-26, x3:25-23, x4:22-18, x5:17-13, x6:12, x7:11, x8:10-6,
x9:5-0

Or

X1:3-0, x2:5-4, x3:8-6, x4:13-9, x5:18-14, x6:19, x7:20, x8:25-21,
x9:31-26

Which is correct?

typdef struct
{
  double        a;
  double        b;
  short int     c;
  short int     d;
  unsigned int  e;
  UINT32        x1 : 4; 
  UINT32        x2 : 2;    
  UINT32        x3 : 3;             
  UINT32        x4 : 5;             
  UINT32        x5 : 5;             
  UINT32        x6 : 1;             
  UINT32        x7 : 1;             
  UINT32        x8 : 5;             
  UINT32        x9 : 6;                      
  unsigned int  f;
  float         g;
} TEST_STRUCT

Thanks,
Phil Crescioli
General Dynamics - Advanced Information Systems
Phil.Crescioli@gd-ais.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Bit Field Formations
  2005-01-21 15:53 Bit Field Formations Crescioli, Phil
@ 2005-01-22  3:50 ` Kevin P. Fleming
  0 siblings, 0 replies; 2+ messages in thread
From: Kevin P. Fleming @ 2005-01-22  3:50 UTC (permalink / raw)
  Cc: gcc-help

Crescioli, Phil wrote:

> If I create a structure that includes bit-field definitions, how are the
> bit fields assigned within a word. In the example below, is variable x1
> assigned to bits 31-28 or bits 3-0? My assumption is that the fields are
> assigned in order so that the example produces either:

I believe that is implementation-defined. The compiler is free to use 
any method it likes to pack the bits into the structure. In fact, the 
identical compiler may organize the bits differently based on the 
architecture's endianness and alignment restrictions.

In other words, bitfields cannot be used reliably (if at all) for things 
that must be communicated outside the process the code is running in 
(except for another process running the same code on the same box, of 
course).

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-01-22  3:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-21 15:53 Bit Field Formations Crescioli, Phil
2005-01-22  3:50 ` Kevin P. Fleming

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).