Bit Fields In C

Example

#include <stdio.h>

struct dob{
    int date;
    int month;
    int year;
};


int main() {
   printf("size of the struct is %ld \n",sizeof(struct dob));
   struct dob myDOB={06,11,2001};
   printf("my dob is %d-%d-%d",myDOB.date,myDOB.month,myDOB.year);
}

Output:

Output

size of the struct is %ld 
my dob is [value]-[value]-[value]

Explanation:

In the example provided, there is a struct data type containing three integer data members: date, month, and year. This struct is designed to hold a person's date of birth, requiring 12 bytes of memory to store the three integers. To optimize memory usage, we can limit the memory allocation by considering the maximum values each data member can hold - 31 for date, 12 for month, and the maximum possible digits for the year.

So, utilizing bit fields helps optimize memory utilization. With bit fields, we have the option to specify the width or range for the data member in bytes.

Syntax:

Example

dataype data_member : maximum_width_bits

Example 2:

Example

#include <stdio.h>

struct dob{
    unsigned int date: 5;
    unsigned int month: 4;
    unsigned int year: 12;
};


int main() {
   printf("size of the struct is %ld \n",sizeof(struct dob));
   struct dob myDOB={06,11,2001};
   printf("my dob is %d-%d-%d",myDOB.date,myDOB.month,myDOB.year);
}

Output:

Output

size of the struct is %ld 
my dob is [value]-[value]-[value]

Explanation:

In the provided code snippet, a bit field was employed within the struct variable. This decision was made based on the understanding that the 'day' variable would not exceed a value of 31, which can be accommodated within 5 bits. Hence, the width of five was deliberately specified for this purpose.

Similarly, we only require 12 values at most in the month section, which can be represented using a maximum of 4 bits (the highest value being 15 with 4 bits).

For the year, we used 12 bits as its width.

So, after determining the total memory taken up by the struct variable, it turned out to be 4 bytes, down from the original 12 bytes. Through the utilization of bit fields, we managed to conserve 8 bytes of memory.

Note: If we give the width to a data member more than its actual range, then it will give the compilation error.

Example 3:

Example

#include <stdio.h>

struct dob{
    unsigned char a: 5;
    unsigned char b: 12;
    unsigned char c: 8;
};


int main() {
   printf("size of the struct is %ld \n",sizeof(struct dob));
   struct dob myDOB={'a','b','c'};
   printf("my dob is %d-%d-%d",myDOB.a,myDOB.b,myDOB.c);
}

Output:

Output

size of the struct is %ld 
my dob is [value]-[value]-[value]

Explanation:

In the provided code snippet, the character data type typically occupies 1 byte or 8 bits of memory. However, in the bit field segment, we specified a width of 12 bits, which surpasses its usual range. As a result, a compilation error will be triggered.

Note: We cannot use the bit field concept in the array.

Example 4:

Example

#include <stdio.h>

struct dob{
    unsigned char a: 5;
    unsigned int arr[12]: 12;
    unsigned char c: 8;
};


int main() {
   printf("size of the struct is %ld \n",sizeof(struct dob));
}

Output:

Output

size of the struct is %ld 
my dob is [value]-[value]-[value]

Explanation:

In the aforementioned scenario, we encountered an issue where a compilation error arose due to using a bit field on an integer array within a structure.

Note: We can use data members without any name with 0 widths to take the memory jump and assign the memory to the following data member after the memory jump.

Example 5:

Example

#include <stdio.h>

struct dob{
    unsigned char a: 5;
    unsigned int : 0;
    unsigned char c: 8;
};


int main() {
   printf("size of the struct is %ld \n",sizeof(struct dob));
}

Output:

Output

size of the struct is %ld 
my dob is [value]-[value]-[value]

Explanation:

In the provided code snippet, there is a single character member followed by an unnamed integer member with a width of 0. As a result, a 4-byte gap will be created before the subsequent character data member is stored in memory.

Note: We cannot get the address of the data member if we use bit fields.

Example 6:

Example

#include <stdio.h>

struct dob{
    int date:5;
    int month:4;
    int year:12;
};


int main() {
   printf("size of the struct is %ld \n",sizeof(struct dob));
   struct dob myDOB={06,11,2001};
   printf("my dob memory address is %p",&myDOB.date);
}

Output:

Output

size of the struct is %ld 
my dob is [value]-[value]-[value]

Explanation:

We encountered an error when attempting to access the address of the bit field data member.

Input Required

This code uses input(). Please provide values below: