Introduction to bit domains in C Language
When storing some information, it does not need to occupy a full byte, but only needs to occupy a few or one binary bit. For example, when storing a switch value, there are only two States: 0 and 1. Use one binary digit.
To save storage space and simplify processing, the C language also provides a data structure called a bit field or bit segment. The bit field divides the binary character in a byte into several different regions and specifies the number of digits in each region. Each domain has a domain name, which allows operations by domain name in the program. In this way, we can use a single byte binary field for several different objects.
. 1. Definition of a bit field and description of a bit field variable the definition of a bit field is similar to that of a structure, in the form:
Struct bit domain structure name
{Bit domain list };
The format of the bit domain list is: type description Character Domain Name: Bit domain Length
Struct WbitF {unsigned int b0: 1;
Unsigned char b0: 1;
Unsigned char b1: 1;
Unsigned char b2: 1;
Unsigned char b3: 1;
Unsigned char b4: 1;
Unsigned char b5: 1;
Unsigned char b6: 1;
Unsigned char b7: 1;
Unsigned char b8: 1;
Unsigned char b9: 1;
Unsigned char b10: 1;
Unsigned char b11: 1;
Unsigned char b12: 1;
Unsigned char b13: 1;
Unsigned char b14: 1;
Unsigned char b15: 1;
}
Each occupies one place. If not, it is saved to the next byte. 1 occupies 2 bytes in total: 1 is stored by bit.
Basic member variables will be split! This syntax is rarely used in elementary programming, but it is constantly used in advanced programming!
Example: struct student {
Unsigned int sex: 1;
Unsigned int age: 15;
};
The two sex and age members in the above struct occupy only one unsigned int space (assuming that the unsigned int is 16 bits ). After the basic member variables are split, the access method is still the same as that when the access is not split, for example, struct student sweek; sweek. sex = MALE; // here, MALE can only be 0 or 1, and the value cannot be greater than 1 sweek. age = 20; although the basic member variables of splitting are syntactically supported, it is not equal to how we divide them. For example, the following split is obviously unreasonable: struct student {unsigned int sex: 1; unsigned int age: 12 ;}; this is because 1 + 12 = 13 and cannot be combined into a basic member, it cannot be combined into char, int, or any type, which obviously cannot be "self-Circular.
Keil does not support binary data, such as 0b0101, so that the compiler cannot pass. When bit fields are used, too many bit operations are used, such as & | <>.
The conversion between byte and bit fields is as follows:
Typedef struct {
U8 model_flag: 2;
U8 fan_rank: 2;
U8 windsweeper: 1;
U8: 3;
} Set_status
Set_status set_info;
Set_info = buff [1]; // assign a byte to the bitfield struct.
Note that the struct is set to a byte that can accommodate the buff [1. Note that the allocation of each variable in the bitfield struct starts from the low position of the byte. If the buff is 0110 1010, model_flag is 10, fan_rank is 10, windsweeper is 0, and unused three bits are 011.