C Programming

BIT FIELDS
Consider the following data elements defined for a PABX telephone system.


	flag = 1 bit
	off_hook = 1 bit
	status =  2 bits

In C, these can be defined as a structure, and the number of bits each occupy can be specified.


	struct packed_struct {
		unsigned int flag:1;
		unsigned int off_hook:1;
		unsigned int status:2;
	} packed_struct1;

The :1 following the variable flag indicates that flag occupies a single bit. The C compiler will assign all the above fields into a single word.

Assignment is as follows,


	packed_struct1.flag = 0;
	packed_struct1.status = 3;
	if( packed_struct1.flag )
		.............


ęCopyright B Brown. 1984-1999. All rights reserved.