Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The C-bit domain


May 11, 2021 C


Table of contents


The C-bit domain

If the structure of the program contains more than one number of switches, only the TRUE/FALSE variable is as follows:

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

This structure requires 8 bytes of memory space, but in practice, we store only 0 or 1 per variable. I n this case, the C language provides a better way to make use of memory space. I f you use such variables within 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 to:

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

Now, in the structure above, the status variable takes up 4 bytes of memory space, but only 2 bits are used to store values. I f you use 32 variables, each with a width of 1 bit, the status structure will use 4 bytes, but as long as you use one more variable, if you use 33 more variables, it will allocate the next segment of memory to store the 33rd variable, which is when you start using 8 bytes. Let's look at the following example to understand the 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 domain declaration

The declaration of bit fields within the structure is as follows:

struct
{
  type [member_name] : width ;
};

Here's a description of the variable elements in the bit field:

element describe
type Integer type determines how to explain the value of the bit field.The type can be integer, a symbol integer, unsigned integer.
member_name The name of the bit field.
width The number of mediated in the bit domain.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 bit field. Bit fields can store more than 1 bit, for example, a variable is required to store values from 0 to 7, and you can define a bit field with a width of 3 bits, as follows:

struct
{
  unsigned int age : 3;
} Age;

The structure definition above indicates to the C compiler that the age variable will only use 3 bits to store this value, which cannot be done if you try to use more than 3 bits. 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 comes with a warning, and when the above code is executed, it produces the following results:

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