From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30130 invoked by alias); 17 Aug 2002 18:58:58 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 30120 invoked from network); 17 Aug 2002 18:58:54 -0000 Received: from unknown (HELO hofr.at) (80.120.128.82) by sources.redhat.com with SMTP; 17 Aug 2002 18:58:54 -0000 Received: (from root@localhost) by hofr.at (8.10.2/8.9.3/SuSE Linux 8.9.3-0.1) id g7HI2wN24657; Sat, 17 Aug 2002 20:02:58 +0200 From: Der Herr Hofrat Message-Id: <200208171802.g7HI2wN24657@hofr.at> Subject: Re: Bitfield packing In-Reply-To: <20020817073337.GA816@fericom.net> from "eddy@fericom.net" at "Aug 17, 2002 09:33:37 am" To: eddy@fericom.net Date: Sat, 17 Aug 2002 11:58:00 -0000 CC: gcc-help@gcc.gnu.org MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-SW-Source: 2002-08/txt/msg00154.txt.bz2 > 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