C Programming Bit Field
In this chapter you will learn the use of bit fields in C programs. Memory can be exploited using bit fields.
Suppose, your program has some TRUE/FALSE variables which are categorized by structure variable and this structure variable is named status. For example:
struct {
unsigned int widthValidated;
unsigned int heightValidated;
} status;
This structure variable will require 8 bytes of space in memory. But we are going to store either 0 or 1 for each variable. C programming language provides good methods for proper use of memory in such situations.
If you use such variables within a structure, you can also specify the memory size or number of bytes for these variables, which will tell the compiler in advance that you are going to use only that number of bytes for these variables. For example, the above structure can also be written as:
struct {
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status;
The above structure variable will occupy 4(four) bytes of space in memory, but only 2 bytes of space will be needed to store the value.
Even if you use up to 32 variables and each one is 1 bit in size, the structure variable status will occupy 4(four) bytes of space in memory. However, as soon as the variable number reaches 33 it will allocate the next memory slot which will start using 8 bytes of memory. Let’s take the idea more clearly through the following example:
#include
#include
/* Define a general structure */
struct {
unsigned int widthValidated;
unsigned int heightValidated;
} status1;
/* Define a structure with bit fields.*/
struct {
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status2;
int main( ) {
printf( "Memory size occupied by status1 : %d\n", sizeof(status1));
printf( "Memory size occupied by status2 : %d\n", sizeof(status2));
return 0;
}
Compiling and executing the above program will produce the following output:
Memory size occupied by status1 : 8
Memory size occupied by status2 : 4
Bit field declaration
The bit field within the structure is declared as follows:
struct {
type member_name : size ;
};
The elements of the bit field are described below:
- type: is the type of the bit field variable. The type is usually
int
,signed
orunsigned int
may be . - member_name is the name of the bit field variable
- size The number of bits in the bit field variable. This size must be smaller than the bit size of the specified type.
A variable defined using a predetermined size is called a bit field. A built field can contain more than one bit.
For example, if you need a variable that can store values from 0 to 7, you can define a 3-bit bit field variable like this:
struct {
unsigned int age : 3;
} Student;
The above program instructs the C compiler that the age variable is going to use 3 bits to store its value. But if you want to use more than 3 bits then it won’t allow you to do this. Let’s see the application with the help of the following example:
#include
#include
struct {
unsigned int age : 3;
} Student;
int main( ) {
Student.age = 4;
printf( "Sizeof( Student ) : %d\n", sizeof(Student) );
printf( "Student.age : %d\n", Student.age );
Student.age = 7;
printf( "Student.age : %d\n", Student.age );
Student.age = 8;
printf( "Student.age : %d\n", Student.age );
return 0;
}
When the above program is compiled it will compile with a warning and when the execution is complete it will show the following output:
Sizeof(Student): 4
Student.age : 4
Student.age : 7
Student.age : 0