public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Bitfield packing
@ 2002-08-17  2:33 eddy
  2002-08-17 11:58 ` Der Herr Hofrat
  0 siblings, 1 reply; 2+ messages in thread
From: eddy @ 2002-08-17  2:33 UTC (permalink / raw)
  To: gcc-help

Hi, 

I am using gcc for a microcontroller application. I want to use
bitfields to describe the processor registers, 
similar to this: 

typedef union {
	IO_BYTE byte; 
	struct {
		IO_BYTE P30 :1; 
		IO_BYTE P31 :1; 
		IO_BYTE P32 :1; 
		IO_BYTE P33 :1; 
		IO_BYTE P34 :1; 
		IO_BYTE P35 :1; 
		IO_BYTE P36 :1; 
		IO_BYTE P37 :1; 
	} bit; 
} PDR2;

How does gcc handle bitfields like thisone? Above I have defined 8 bits,
are they distributed on several bytes with some optimization levels?

How do I setup gcc, so that it will always assign the bits 1:1 (no
packing/optimization)?

Thanks


Eddy Ilg

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

* Re: Bitfield packing
  2002-08-17  2:33 Bitfield packing eddy
@ 2002-08-17 11:58 ` Der Herr Hofrat
  0 siblings, 0 replies; 2+ messages in thread
From: Der Herr Hofrat @ 2002-08-17 11:58 UTC (permalink / raw)
  To: eddy; +Cc: gcc-help

> Hi, 
> 
> I am using gcc for a microcontroller application. I want to use
> bitfields to describe the processor registers, 
> similar to this: 
> 
> typedef union {
> 	IO_BYTE byte; 
> 	struct {
> 		IO_BYTE P30 :1; 
> 		IO_BYTE P31 :1; 
> 		IO_BYTE P32 :1; 
> 		IO_BYTE P33 :1; 
> 		IO_BYTE P34 :1; 
> 		IO_BYTE P35 :1; 
> 		IO_BYTE P36 :1; 
> 		IO_BYTE P37 :1; 
> 	} bit; 
> } PDR2;
> 
> How does gcc handle bitfields like thisone? Above I have defined 8 bits,
> are they distributed on several bytes with some optimization levels?

if you don't use any __attribute__((packed)) on the bitfields the size of
this object will be alligned machin dependant (probably 32bit on a 32bit box)
you can see that by using the __alignof__() (just like sizeof) function to
report allignement - internal ordering I guess is nothing you should ever 
relie on. See the section "storage layout" in the gcc manual for infos on
what may influence your setup (i.e. BITS_PER_UNIT).

If you want to be shure of the bit-positions I guess you must use 
bit-operators and allocate an unsigned char / u8 object.

hofrat

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

end of thread, other threads:[~2002-08-17 18:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-17  2:33 Bitfield packing eddy
2002-08-17 11:58 ` Der Herr Hofrat

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