Bit fields in Struct

In structure, there are many variables. Some of them are not even used. And some of them are used partially. This means if there is a variable of int data type then it occupies 2 bytes in memory. But suppose if the value which we are storing in it is of only 1 byte so the remaining 1 byte is just a waste of memory. 

As the solution to this, we can use the bit fields concept in structure. In this, with structure members we can define the number of bites, so the size of that structure member will be according to that defined number of bits. Syntax to declare the number of bits is as given below.

<data type of member >  <member name>  :  <number of bits> ;

Let us understand it by an example. Suppose below members are declared in a structure.

int var1,var2; //structure is having these members
  • So total size of structure is = size of var1(2 bytes) + size of var2(2bytes) 

                 = 4 byte =32 bits

But if we want to store only 4 bits of data in var1 and 3 bits of data in var2. So if we want to save memory, we can write the structure member declaration below using bit fields.

int var1 : 4 ,var2 : 3 ; //structure is having these members

Here 4 and 3 shows that var1 occupies 4 bits (this is the same as a normal size of int ) and var2 occupies 3 bits. So this needs 4+3 = 7 bits.

In actually, it does not occupy the memory of 7 bits. Because for this type, there are 2 blocks and both are having the size of 2 bytes(16 bits).
Here 2 blocks are created because there are two members, and both are of 2 bytes because var1, var2 members are of int data type and size of int is 2 bytes. Let us understand the diagram below.

Here these blocks can not be broken. If a block is filled half (or partially filled) then we have to consider it as a full block. In short, in the above example, the size was 7 bits but there are blocks of 2 bits. One block is partially filled (which is shown by green color). So we have to take it as a full block. So the size of this structure is = size of a block = 2 bytes = 16 bits.

 

int var1 : 14 ,var2 : 5 ; //structure is having these members

In this case total bits by members = 14 + 5 = 19 bits = 2 bytes and 3 bits

Here it has a total of 19 bits but we can not break any block. One block is fully filled and the 2nd block is partially filled. But we have to take the 2nd block as full because we are not allowed to break any block. So the size of this structure is 4 bytes.

Below is a simple program of printing the size of the structure. Here structure str has 3 members : month1, month2, month3. Variable of structure str is str1. So size of structure = size of month1 + size of month2 +size of month 3 = 2 bytes + 2 bytes + 2 bytes = 6 bytes

#include <stdio.h>
#include <conio.h>
struct str
{
int month1,month2,month3;};

void main()
{
    struct str str1;
    clrscr();
    printf("%d",sizeof(str1));
    getch();
}

Output:

Below is the same program as above but using bit fields. Size of structure = size of month1 + size of month2 +size of month3 = 4bits + 4bits + 4bits =  12 bits (but we can not break blocks so actual size = 16 bits = 2 bytes)

#include <stdio.h>
#include <conio.h>
struct str
{ 
int month1 : 4,month2 : 4,month3 : 4;	
};
void main()
{ 
  struct str str1;
  clrscr();
  printf("%d",sizeof(str1));
  getch();
}

Output:

Learning from this blog:

  1. Meaning of bit field
  2. Why do we need this concept?
  3. Difference between simple structure member declaration and bit field structure declaration.
  4. Syntax of using bit field
  5. What do you mean by the statement “Can not break blocks”.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments