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:

  • Need for Bit Fields
  • Declaration  of Bit Fields
  • Working of Bit Fields

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

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

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.

Bit Fields in C Language

Need for Bit Fields in C

Bit fields are of great significance in C programming, because of the following reasons:

  • Used to reduce memory consumption.
  • Easy to implement.
  • Provides flexibility to the code.

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

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.

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

Code on Screen

How to implement Bit fields in C

Output

Output of Implementation of Bit fields in C

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!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.