Site icon DataFlair

Bit Fields in C – An Unrecognised Concept Omitted by C Aspirants

Bit fields in C are relatively very simple than all the topics we have covered so far. A bit field is simply a data structure that helps the user to allocate memory to structures and unions.

In this tutorial, we will discuss:

In order to understand bit fields, we need to have a clear understanding of Structures and Unions in C.

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:

Declaration of Bit Fields in C

A bit field is pretty easy to declare. Its declaration is as follows:

struct
{
data_type variable_name : size_in_bits;
};

The formal name for size_in_bits is called the width of the bit field.

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.

Since an unsigned integer occupies 4 bytes of memory according to a 64-bit compiler, the size of the structure would be 12 bytes.

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

Output

Therefore, by the implementation of bit fields, we can restrict their sizes.

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.

If you are struggling with Array in C, this is the right place to strengthen your fundamentals.

Code on Screen

Output

Summary

In this tutorial, we discussed what are bit fields, its significance, and working. We have moved a step forward in a better understanding of the C programming language by completing this tutorial.

Comment below your experience after exploring the underrated topic- bit fields in C. We have something that might fascinate you even more – Multi-dimensional Arrays in C

See you around!

Exit mobile version