c language lets us do this in a structure


C language lets us do this in a structure definition by putting: bit length after the variable that is.

struct packed_struct {
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int funny_int:9;
} pack;

Here the packed_struct contains 6 members: Four 1 bit flags f1..f3, a 4 bit type and a 9 bit funny_int.

C language automatically packs the above bit fields as compactly as possible provided that the maximum length of the field is equal or less than to the integer word length of the computer. If this isn't the case then some compilers may permit memory overlap for the fields whilst other would store the next field in the next word.

Access members as usual via:

pack.type = 7; 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: c language lets us do this in a structure
Reference No:- TGS0305231

Expected delivery within 24 Hours