Bit domain
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 "bit domain" or "bit segment ". The so-called "bit field" refers to dividing the binary character in a byte into several different regions and showing the digits of each region. Each domain has a domain name, which allows operations by domain name in the program. In this way, several different objects can be represented by a byte binary field.
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
For example:
Struct BS
{
Int A: 8;
Int B: 2;
Int C: 6;
};
The description of bitfield variables is the same as that of structure variables. You can first define and then describe, and define or directly describe these three methods. For example:
Struct BS
{
Int A: 8;
Int B: 2;
Int C: 6;
} Data;
It indicates that data is a BS variable, which occupies two bytes in total. Where a occupies 8 places, B occupies 2 places, and C occupies 6 places. The definitions of bit domains are described as follows:
1. A single-byte field must be stored in the same byte, and cannot span two bytes. If the remaining space of one byte is insufficient to store another domain, it should be stored from the next unit. You can also intentionally start a domain from the next unit. For example:
Struct BS
{
Unsigned A: 4
Unsigned: 0/* airspace */
Unsigned B: 4/* stored from the next unit */
Unsigned C: 4
}
In the definition of this bit field, a occupies 4 bits in the first byte, And the last 4 bits enter 0 to indicate that it is not used. B starts from the second byte and occupies 4 bits, and C occupies 4 bits.
2. The length of a bit field cannot exceed the length of one byte, that is, it cannot exceed 8 bits.
3. A bit domain can be a non-bit domain name. In this case, it is only used for filling or adjusting the position. An anonymous domain cannot be used. For example:
Struct K
{
Int A: 1
INT: 2/* The two digits cannot be used */
Int B: 3
Int C: 2
};
From the above analysis, we can see that the bit field is essentially a structure type, but its members are allocated by binary.
Ii. Use of bit Domains
The usage of bit domains is the same as that of structure members. Generally, the form of bit domain variable name-bit domain name can be output in various formats.
Main (){
Struct BS
{
Unsigned A: 1;
Unsigned B: 3;
Unsigned C: 4;
} Bit, * pbit;
Bit. A = 1;
Bit. B = 7;
Bit. c = 15;
Printf ("% d, % d, % d/N", bit. A, bit. B, bit. C );
Pbit = & bit;
Pbit-> A = 0;
Pbit-> B & = 3;
Pbit-> C | = 1;
Printf ("% d, % d, % d/N", pbit-> A, pbit-> B, pbit-> C );
}
In the preceding example, the bit domain structure Bs is defined. The three bit domains are A, B, and C. This section describes the BS type variable bit and the BS type pointer variable pbit. This indicates that pointers can also be used for bit fields.
The program's Lines 9, 10, and 11 assign values to the three single-digit domains. (Note that the value assignment cannot exceed the permitted range of the bit field) The program outputs the content of the three fields in integer format in line 1. Row 3 sends the bit address of the bit field variable to the pointer variable pbit. Row 14th re-assigns a value to bit field A as a pointer and assigns it to 0. Row 15th uses the compound bitwise operator "& =", which is equivalent to 7 in the original value of pbit-> B = pbit-> B & 3-bit Domain B, the bitwise AND operation result of 3 is 3 (111 & 011 = 011, And the decimal value is 3 ). Similarly, the Code uses the compound bitwise operation "| =" in line 1, which is equivalent to pbit-> C = pbit-> C | 1 and the result is 15. The program output the values of the three fields in the pointer mode in Row 3.
To save space, you can compress several data types to a few types of space. For example, you need to indicate two three-bit binary numbers and one two-bit binary number, it can be represented by an 8-bit character.
Struct
{
Char A: 3;
Char B: 3;
Char C: 2;
};
This struct occupies 8 bytes of space. Saves space.
Union
Union is a special category defined by the keyword Union. Union maintains sufficient space to place "one" among multiple data members rather than configuring space for each data member, all data members in Union share one space, and only the data of one member can be stored at the same time. An example of a Union definition is as follows:
Union statemachine {
Char character;
Int number;
Char * STR;
Double exp;
};
A union can only be configured with a large enough space to accommodate the maximum length of data members. In the preceding example, the maximum length is the double type, so the memory space of statemachine is the length of the double type.