Bitfields
- Data items which can be combined to form an individual word of memory are known as bitfields.
- In the above syntax, each member is followed by a semicolon and an unsigned integer which indicates the field size.
- The definition and accessing of every member is the same as the ordinary structure.
- The above structure is subdivided into 3 bit fields. Their widths are 1 and 3 so they occupy 4 bits within the word of a memory.
- The values to bitfields are assigned using the #define.
- They can also be initialized and even appear in arithmetic expressions.
- Bitfields of arrays are not allowed.
- The '&' operator cannot be added to a bitfield.
- A pointer cannot access a bitfield and a function cannot return a bitfield.
- A bitfield without a name can be used for padding.
- It can be forced for starting of a new beginning by specifying an unnamed bitfield with 0 width.
Syntax:
struct struct_name
{
member 1 : size;
member 2 : size;
.
.
member n : size;
};
Example:
We want to store the details of a student. The information can be:
i) Roll no of a student where 1 bits is sufficient.
ii) Standard or class that it belongs to so 3 bits would be sufficient.
The structure can be declared as follows:
struct student
{
unsigned roll_no: 1;
unsigned std : 3;
};


