Bit Fields in C

The C programming language has a lot of features available to the programmers. The C programming language helps programmers in creating their program code simple and easy to understand. It helps you in a better way to utilize the memory space in your program code. With the help of bit fields, you can achieve it. In coding, it is important to make use of the memory space in your program code.

What is a bit field in C?

In the C programming language, you can define the size(in bits) of the structure and union members. It is useful when you want to use the memory efficiently by knowing that the value of the field or the collection of the fields will never exceed a specific limit or it is in-between a small range.

How does bit field work in C?

Let’s take an example to better understand the working of bit fields. Below is an example of declaring the date without the use of bit fields:-

#include <stdio.h>
struct timing{
    unsigned int date;
    unsigned int month;
    unsigned int year;
};
int main()
{
  printf("TechVidvan Tutorial: Bit Fields in C!\n\n");
    printf("Size of timing is %d bytes\n",sizeof(struct timing));
    struct timing t1 = { 8, 10, 2000 };
    printf("Some random date: %d/%d/%d", t1.date, t1.month, t1.year);
}

Output:-

TechVidvan Tutorial: Bit Fields in C!

Size of timing is 12 bytes
Some random date: 8/10/2000

In the above example, the total size of struct type timing is 12 bytes because the size of unsigned int is 4 bytes. On the other hand, we know that the value of date is always from 1 to 31 and also the value of month is always from 1 to 12. Using bit fields, we can utilize the space.

But if we use the above program using signed int and the bit fields then you could see something interesting in your program.

Follow the below example:-

#include <stdio.h>
struct timing{
  int date : 5; // date has value from 1 to 31, so 5 bits should be enough!
  int month : 4; // month has value from 1 to 12, so 4 bits should be enough!
  int year;
};
int main()
{
  printf("TechVidvan Tutorial: Bit Fields in C!\n\n");
    printf("Size of timing is %d bytes\n",sizeof(struct timing));
    struct timing t1 = { 16, 10, 2000 };
    printf("Some random date: %d/%d/%d", t1.date, t1.month, t1.year);
}

Output:-

TechVidvan Tutorial: Bit Fields in C!

Size of timing is 8 bytes
Some random date: -16/-6/2000

Let me explain how we got a negative value in the above example.

The value 31 is being stored in a 5 bit integer. This 5 bit integer is equal to 11111. In that case, MSB is 1 and it is a negative number. If you calculate the 2’s complement of 11111 then it would be equal to 00001. And 00001 is equal to decimal number 1. That’s why, you will get a -1.

In a similar way, 4 bit representation of 12 is equal to 1100. And if you calculate the 2’s complement of 1100 then you will get -4.

Declaration of bit fields in C

You can declare a bit field inside a structure.

Syntax:-

struct {
   data_type [member_name] : width;
};

Example:-

struct {
   unsigned int age : 5;
} each_age;


  • data_type defines the type of data which can be integer, signed integer or unsigned integer.
  • member_name defines the name of the bit-field member inside the structure.
  • width is the number of bits required in the bit field.

Example of Basic bit field program

#include <stdio.h>
struct {
  int age : 5;
}each_age;
int main(){
  printf("TechVidvan Tutorial: Example of bit field!\n\n");
   each_age.age = 11;
   printf("First person's age is: %d\n", each_age.age );
   each_age.age = 10;
   printf("Second person's age is: %d\n", each_age.age );
   return 0;
}

Output:-

TechVidvan Tutorial: Example of bit field!

First person’s age is: 11
Second person’s age is: 10

Some important points about bit fields in C:-

You cannot use pointers to the bit field member.

For Example:-

#include <stdio.h>
struct no_pointer {
  unsigned int a : 4;
};
int main()
{
  struct no_pointer point;
  printf("Address of point.a is %p", &point.a);
  return 0;
}

Output:-

main.c: In function ‘main’:
main.c:8:40: error: cannot take address of bit-field ‘a’
printf(“Address of point.a is %p”, &point.a);

Array of bit fields is not acceptable.

For Example:-

struct array {
int arr[10] : 3;
};
 
int main()
{
}

Output:-

main.c:2:5: error: bit-field ‘arr’ has invalid type
int arr[10] : 3;
^~~

Bit size of 0 is used to force alignment on the next boundary.

For Example:-

#include <stdio.h>
//without forced alignment
struct case_1{
  int a : 4;
  unsigned int b : 5;
};
 
//with forced alignment
struct case_2{
  int a : 6;
  int : 0;
  unsigned int b : 4;
};
 
int main()
{
  printf("Size of structure without forced alignment is: %u bytes\n",sizeof(struct case_1));
  printf("Size of structure with forced alignment is: %u bytes\n",sizeof(struct case_2));
  return 0;
}

Output:-

Size of structure without forced alignment is: 4 bytes
Size of structure with forced alignment is: 8 bytes

Applications of bit fields in C

Bit fields are more useful when multiple devices transfer information encoded into multiple bits. You can make use of bit fields when storage is limited.

Need of bit fields in C

  • Easy to implement.
  • Reduce memory consumption.
  • Provides efficiency and flexibility to the program.

Summary

In this tutorial, we learnt about what bit fields are and how bit fields work. We also discussed the applications and needs of bit fields. We also discussed how you can declare bit fields.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.