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

Get Certified in C Programming and Take Your Skills to the Next Level

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.

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;
};

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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!

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

10 Responses

  1. Ashwin says:

    Minutes and seconds: Since the range is from 0-59, we consider 6 bits as 26 = 64

    What is 26=64 ?

  2. Marinho says:

    2^6 = 64 bits
    If you are at least a little bit familiar with binary numbers, you should have guessed that…

  3. Nakul Audeechya says:

    Bit field used are 5, 6, 6 = 17 bits, therefore total bytes shoud be = 3 bytes.
    or
    if consider min 1 byte for 1 member then also 5,6,6, = 1+1+1 = 3 bytes.

    How 4 bytes is coming?

    • Jeba says:

      I was wondering the same thing bro. I don’t get it. I agree with you.

      • RAK ACOUSTIC says:

        This is because many systems align data to 4-byte boundaries for performance reasons. So, even though the data technically fits into 3 bytes, the compiler will add an extra byte of padding to align the struct to a 4-byte boundary, resulting in a total size of 4 bytes

    • RAK ACOUSTIC says:

      This is because many systems align data to 4-byte boundaries for performance reasons. So, even though the data technically fits into 3 bytes, the compiler will add an extra byte of padding to align the struct to a 4-byte boundary, resulting in a total size of 4 bytes

    • RAK ACOUSTIC says:

      Your reason is right, but this happent depend due to compiler rule

  4. Gursewak Singh says:

    Because an unsigned integer has 4 bytes of size when it comes to 32/64 bit compiler.
    So, allocated 4 bytes i.e. 32bits memory so 17bits are accommodated in it.

  5. Jeba says:

    I was wondering the same thing bro. I don’t get it. I agree with you.

Leave a Reply

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