Bit Fields in C – An Unrecognised Concept Omitted by C Aspirants
A bit field is simply a data structure that helps the user to allocate memory to structures and unions.
Bit Fields in C Language
In programming terminology, a bit field is a data structure that allows the programmer to allocate memory to structures and unions in bits in order to utilize computer memory in an efficient manner.
Since structures and unions are user-defined data types in C, the user has an idea of how much memory will they occupy. Accordingly, by the implementation of bit fields, memory management becomes easy and efficient.
Need for Bit Fields in C
Bit fields are of great significance in C programming, because of the following reasons:
1.Used to reduce memory consumption.
2.Easy to implement.
3.Provides flexibility to the code.
Declaration on bit fields in c:
A bit field declaration is as follows:
struct
{
data_type variable_name : size_in_bits;
};
How do Bit Fields in C works?
In order to understand how bit fields work, let us consider a problem, in which we are expected to define a structured time to display the time according to 24-hour clock entered by the user with unsigned int hours, minutes and seconds.
Here is a code in C that illustrates the implementation of a structured time without the use of bit fields:
#include <stdio.h>struct time
{
unsigned int hours;
unsigned int minutes;
unsigned int seconds;
};
int main()
{
struct time t = {11, 30, 10}; // Here t is an object of the structure time
printf("Welcome to DataFlair tutorials!\n\n");
printf("The time is %d : %d : %d\n", t.hours, t.minutes, t.seconds);
printf("The size of time is %ld bytes.\n", sizeof(struct time));
return 0;
}
Clearly, we know that, for a 24-hour clock, the range of hours should be from 0 to 23, minutes, and seconds should be from 0 to 59.
Code on Screen
How bit fields in C works
Output
Output of Bit fields in C
Therefore, by the implementation of bit fields, we can restrict their sizes.
Hours: Since the range is from 0-23, we consider 5 bits as 25 = 32 which is the nearest larger bit than the upper limit. If we consider 4 bits, then 24 = 16 would be smaller than the upper limit.
Minutes and seconds: Since the range is from 0-59, we consider 6 bits as 26 = 64 which is the nearest larger bit than the upper limit. If we consider 5 bits, then 25 = 32 would be smaller than the upper limit.
Here is a code in C that illustrates the use of bit-fields with the help of the previous example:
#include <stdio.h>struct time
{
unsigned int hours: 5; // Size restricted to 5 bits
unsigned int minutes:6; // Size restricted to 6 bits
unsigned int seconds:6; // Size restricted to 6 bits
};
int main()
{
struct time t = {11, 30, 10}; // Here t is an object of the structure time
printf("Welcome to DataFlair tutorials!\n\n");
printf("The time is %d : %d : %d\n", t.hours, t.minutes, t.seconds);
printf("The size of time is %ld bytes.\n", sizeof(struct time));
return 0;
}
It is important to note that in C bit fields cannot be declared as static, that is, the data type modifier static cannot be used. Another important thing to keep in mind is that an array of bit fields do not exist and hence can’t be implemented.