bit field initialization question

utzoo!decvax!genrad!john utzoo!decvax!genrad!john
Wed Apr 27 10:13:56 AEST 1983


With the example bit field structure presented by ogcvax!sbq:

	struct byt {
		unsigned b1  : 1;
		unsigned b2a : 2;
		unsigned b3  : 3;
		unsigned b2b : 2;
		};

the bit fields are packed into an unsigned.  Therefore, each unit of
"struct byt" is "sizeof (unsigned)" long.  Note that K&R states on
page 197 that "implementations are not required to support any but
integer fields."

Therefore, the following will generate true packed data on 4.1BSD but it
may not be portable to other machines/compilers:

	struct byt {
		unsigned char b1  : 1;
		unsigned char b2a : 2;
		unsigned char b3  : 3;
		unsigned char b2b : 2;
		};

	struct byt xx [] = {
	{1,0,2,0},  /* 11 */
	{0,1,4,0},  /* 22 */
	{1,1,6,0},  /* 33 */
	{0,2,0,1},  /* 44 */
	{1,2,2,1},  /* 55 */
	{0,3,4,1},  /* 66 */
	{1,3,6,1},  /* 77 */
	{0,0,1,2}}; /* 88 */

/** ----------  ccom output -------------

LL0:
	.data
	.data
	.globl	_xx
_xx:
	.long	0x44332211
	.long	0x88776655
	.data



More information about the Comp.lang.c mailing list