Latest web development tutorials

C bit fields

If the structure of the program contains more than one digital only TRUE / FALSE variables, as follows:

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

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

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

Now, the above structure, status variable occupies four bytes of memory, but only two are used to store values. If you use 32 variables, each variable width of one, then the status structure will use 4 bytes, but more as long as you use a variable, if you use 33 variables, it will be under a section of memory allocated the first 33 variables are stored, this time to start using 8 bytes. Let's look at the following examples 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 : 8
Memory size occupied by status2 : 4

Bit field declaration

In the bit field structure declaration is as follows:

struct
{
  type [member_name] : width ;
};

The following is a description of the bit field variable element:

元素描述
type整数类型,决定了如何解释位域的值。类型可以是整型、有符号整型、无符号整型。
member_name位域的名称。
width位域中位的数量。宽度必须小于或等于指定类型的位宽度。

Variables with predefined width is calleda bit field.Bit fields can store more than one number, for example, need a variable to store the value from 0 to 7, you can define a width of three bit fields as follows:

struct
{
  unsigned int age : 3;
} Age;

Definition of the structure above directions C compiler, age will only use three variables to store this value, if you try to use more than three, you can not complete. Let's look at the following examples:

#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 carry a warning, when the above code is executed, it will produce the following results:

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