A bit-field is a special feature of C structures that allows a member to use a specified number of bits instead of using the normal size of its data type. Bit-fields are useful when a variable needs to store only a small range of values.
A bit-field is a special feature of C structures that allows a member to use a specified number of bits instead of using the normal size of its data type. Bit-fields are useful when a variable needs to store only a small range of values.
A 1-bit field can represent two states, such as
0 and 1.
If a variable needs only a few possible values, using a complete integer for it may use more memory than necessary. Bit-fields allow the programmer to specify only the number of bits required for that particular member.
If a variable can have only two values, 0 and
1, one bit is sufficient to represent it.
The number of bits determines how many different values can be represented. With n bits, up to 2n different combinations can be represented.
| Bits | Possible Values |
|---|---|
| 1 bit | 2 values |
| 2 bits | 4 values |
| 3 bits | 8 values |
| 4 bits | 16 values |
If a value can range from 0 to 3,
four different values are possible, so 2 bits
are sufficient.
A bit-field is declared inside a structure by writing the data type, member name, and the number of bits after a colon.
Bit-fields are generally used as members of a structure. Different members can be assigned different numbers of bits according to the values they need to represent.
Here, each member is given a specific number of bits according to the amount of information it needs to store.
Suppose an employee record needs to store whether the employee is male or female, the marital status, a hobby category, and a scheme number. These fields can be represented using different numbers of bits instead of using separate full-size integer members.
The number after : specifies the number of bits
assigned to each member.
Write a C program using bit-fields to store simple employee information such as gender, marital status and hobby, and display the stored values.
Define an Employee structure with bit-fields for
gender, marital status and hobby. Assign values and display them.
Bit-fields allow a structure member to occupy a specified number of bits. This is useful for data whose possible values are limited, because only the required number of bits needs to be allocated.
Gender = 1
Marital Status = 2
Hobby = 5
In the program, gender uses 1 bit,
maritalStatus uses 2 bits, and hobby
uses 3 bits. The bit-field width limits the range of values that
can be stored in each member.
| Advantage | Explanation |
|---|---|
| Memory Efficiency | Only the specified number of bits is allocated to a field. |
| Compact Data | Useful for storing several small values together. |
| Useful for Flags | Suitable for values that represent ON/OFF or similar states. |
| Structured Representation | Related bit-level information can be grouped in a structure. |
Bit-fields are useful for compact data storage, but their exact memory layout and some implementation details can depend on the compiler. Therefore, they should be used carefully when a program must depend on a particular memory representation.
Bit-fields are mainly useful when the range of possible values is known and small.
| Concept | Remember |
|---|---|
| Bit-field | Structure member with a specified number of bits. |
| Syntax |
type member : width;
|
| 1 Bit | Can represent 2 possible combinations. |
| 2 Bits | Can represent 4 possible combinations. |
| Use | Useful for compact storage of small-range values. |
Watch a beginner-friendly explanation of bit-fields in C.
A short handwritten-style revision sheet for bit-fields will be provided here.
Use the mind map for quick revision of bit-field width, structure declaration and memory efficiency.