C bit field


If the program structure contains multiple switches, there are only TRUE/FALSE variables, as follows:

struct{  unsigned int widthValidated;  unsigned int heightValidated;} status;

This structure requires 8 bytes of memory space, but in fact, in each In each variable, we only store 0 or 1. In this case, C language provides a better way to utilize memory space. If you use such a variable inside a structure, you can define the width of the variable to tell the compiler that you will only use these bytes. For example, the above structure can be rewritten as:

struct{  unsigned int widthValidated : 1;  unsigned int heightValidated : 1;} status;

Now, in the above structure, the status variable will occupy 4 bytes of memory space, but only 2 bits are used to store the value. If you use 32 variables, each variable is 1 bit wide, then the status structure will use 4 bytes, but as soon as you use one more variable, if you use 33 variables, then it will allocate the next section of memory. To store the 33rd variable, 8 bytes will be used at this time. Let us look at the following example to understand this concept:

#include <stdio.h>#include <string.h>/* 定义简单的结构 */struct{  unsigned int widthValidated;  unsigned int heightValidated;} status1;/* 定义位域结构 */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;}

When the above code is compiled and executed, it produces the following results:

Memory size occupied by status1 : 8Memory size occupied by status2 : 4

Bitfield declaration

The form of declaring bit fields within the structure is as follows:

struct{
  type [member_name] : width ;};

The following is a description of the variable elements in the bit field:

ElementsDescription
typeThe integer type determines how to interpret the value of the bit field. The type can be integer, signed integer, or unsigned integer.
member_nameThe name of the bit field.
widthThe number of bits in the bit field. The width must be less than or equal to the bit width of the specified type.

A variable with a predefined width is called a bitfield. Bit fields can store more than 1 digit. For example, if you need a variable to store values ​​from 0 to 7, you can define a bit field with a width of 3 bits, as follows:

struct{  unsigned int age : 3;} Age;

The above structure definition Instructs the C compiler that the age variable will only use 3 bits to store this value, and if you try to use more than 3 bits, it won't work. Let's look at the following example:

#include <stdio.h>#include <string.h>struct{  unsigned int age : 3;} Age;int main( ){   Age.age = 4;
   printf( "Sizeof( Age ) : %d\n", sizeof(Age) );
   printf( "Age.age : %d\n", Age.age );   Age.age = 7;
   printf( "Age.age : %d\n", Age.age );   Age.age = 8;
   printf( "Age.age : %d\n", Age.age );   return 0;}

When the above code is compiled, it will come with a warning. When the above code is executed, it will produce the following results:

Sizeof( Age ) : 4Age.age : 4Age.age : 7Age.age : 0