Path: utzoo!utgpu!watserv1!watmath!att!rutgers!mit-eddie!bu.edu!xylogics!samsung!sdd.hp.com!hp-pcd!hpfcso!mike From: mike@hpfcso.HP.COM (Mike McNelly) Newsgroups: comp.sys.hp Subject: Re: Re: Bit field alignment on HP-UX Message-ID: <7370172@hpfcso.HP.COM> Date: 16 Jul 90 17:31:01 GMT References: <3598@ruuinf.cs.ruu.nl> Organization: Hewlett-Packard, Fort Collins, CO, USA Lines: 67 > / hpfcso:comp.sys.hp / piet@cs.ruu.nl (Piet van Oostrum) / 2:16 am Jul 13, 1990 / > I am still trying to get the whole scoop on bit-field alignment in the > HP-UX cc compiler on HP9000/300. I detected another case where the > bit-field was aligned, which is actually more in line with what others do - > if there were not a case that I don't understand - > > struct test { > /* char p;*/ > int a:14; > int b:20; > short c:5; } x; > > This case (with the char p commented out) aligns b on a 2-byte boundary, > presumably because it would otherwise cross a 4-byte boundary, although > 4-byte boundaries are not special in records. But if the char p is > inserted, no padding is done, the field a doesn't even start on a 2-byte > boundary. Now I don't understand what the underlying algorithm is. This > might even be a bug. > -- > Piet* van Oostrum, Dept of Computer Science, Utrecht University, > Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands. > Telephone: +31-30-531806 Uucp: uunet!mcsun!ruuinf!piet > Telefax: +31-30-513791 Internet: piet@cs.ruu.nl (*`Pete') > ---------- The best and most current source of information about bitfields on Series 300 is the HP-UX Portability Guide, HP Part Number 98794-90046. The information below was an original source for that discussion although I believe that signed bitfields are now supported. Mike McNelly mike%hpfcla@hplabs.hp.com ======================================================================= Bitfields are assigned left to right and are unsigned regardless of the declared type. They are aligned so that they do not violate the alignment restriction of the declared type. Consequently some padding within the structure may be required. For example, struct foo { unsigned int a:3, b:3, c:3, d:3; unsigned int remainder:20; }; For the above struct, sizeof(struct foo) would return 4 (bytes) because none of bitfields straddle a 4 byte boundary. On the other hand, the following struct declaration will have a larger size. struct foo2 { unsigned char a:3, b:3, c:3, d:3; unsigned int remainder:20; }; In this struct declaration the assignment of data space for c must be aligned so as not to violate a byte boundary, which is the normal alignment of unsigned char. Consequently two undeclared bits of padding are added by the compiler so that c is aligned on a byte boundary. sizeof(struct foo2) therefore returns 6 (bytes). Bitfields on Series 200, 300, and 500 machines cannot exceed the size of the declared type in length. Therefore the largest possible bitfield is 32 bits. All scalar types are permissable to declare bitfields, including enums. However, they are inherently unsigned.