After reading relevant materials and understanding, I will summarize the bit domains in C language. I have not said much about the definition of bit domains here, here I will summarize the system where there is ambiguity in the counterpoint domain. I personally think that the summary is complete. If there are incomplete or inappropriate information, you can leave a message.
1. A bit field is also called a bit field. It is a special structure member or union member (that is, it can only be used in a structure or union ), it is used to specify the number of digits occupied by the member during memory storage, so that the data can be expressed more compact within the host.
2. The usage of bit domains mainly occurs in the following two situations:
(1) When the machine has less available memory and the bit domain can save a lot of memory. For example, when the structure is used as an element of a large array.
(2) When a structure or combination needs to be mapped to a predetermined organizational structure. For example, when you need to access the specific location in the byte.
3. to specify a member as a bit field, the type can only be int, unsigned int, or signed Int. Note: The Int type usually represents the natural length of integers in a specific machine. The short type is usually 16 bits, the long type is usually 32 bits, and the int type can be 16 bits or 32 bits. each compiler can select a proper type Length Based on hardware features. see the C programming language Chinese p32 ).
4. The storage mode of the bit domain structure in the memory depends on the specific Compilation Program. They can be stored either from left to right or from right to left.
5. Common members can also be described in a strongly constructed or federated separator that contains a field description. For example:
Struct ST1
{
Unsigned A: 7;
Unsigned B: 4;
Unsigned C: 5;
Int I; // I is a common member, which is stored in the next word, that is, word alignment.
};
6.int values cannot be stored across characters, and it is best not to store the same bit domain across words (meaning: the number of allocated bits of each domain must be within 16 or 32 bits, if the compiler assigns an int value of 16 bits, the value must be less than 16 bits. If the value is greater than 16 bits, it is recommended that the remaining bit fields be empty and allocated from the next word ).
7. it has special purposes to use an anonymous field with a special width of 0 or a length of 0. it is used to indicate that the two fields or members before and after the domain are separated in two words, that is, the next field located after the domain is stored from the next word.
8. Bit domain remarks:
Note the following points for bit domains:
First, the length of the bit field cannot exceed the number of characters occupied by the int object. For example, if the int object occupies 16 bits, the following bit field descriptions are incorrect:
Unsigned int X: 17;
Second, because the implementation of bit domains varies with the Compilation Program, using bit domains here will affect the program portability, it is best not to use a bit domain when it is not necessary to use a bit domain.
Third, although the bit domain can save memory space, it increases the processing time, when accessing members of each bit domain, you need to break down the bit domain from its word or, in turn, compress one value to the character bit where the domain is located.
Fourth, the location of the bit domain cannot be accessed, because some do not use the address operator number for the counterpoint domain & (this operator can be used for non-bit domain members ). therefore, you cannot use a pin pointing to a bit domain or an array of a bit domain (because an array is actually a special pointer ). in addition, the bitfield cannot be returned by the function.
The last step is to emphasize that a bit field, also called a bit field, is a special structure member or federated member (that is, it can only be used in a structure or union ).
Note: most of the above texts are excerpted from the ansi c standard details and the C programming language Chinese new version. I would like to express my gratitude to the above two reference books.
Acceleration march.6.2010
Original article: http://blog.21ic.com/user1/6121/archives/2010/67210.html