Bit structure
A bit structure is a special structure that is more convenient for bit structures than bitwise operators when it is necessary to access multiple bits of a byte or word by bit.
The general form of a bit structure definition is:
struct bit struct name {
Data type variable name: integer constant;
Data type variable name: integer constant;
} bit structure variable;
Where: The data type must be int (unsigned or signed). The integer constant must be a nonnegative integer, the range is 0~15, which represents the number of bits, that is, how many bits.
The variable name is the selection and can be not named, so the rule is to arrange the need.
For example, a bit structure is defined below.
struct{
unsigned incon:8; /*incon occupies a low byte 0~7 total of 8 bits */
Unsigned txcolor:4;/*txcolor occupies a high-byte 0~3 of 4 bits */
Unsigned bgcolor:3;/*bgcolor occupies a high-byte 4~6 of 3 bits */
unsigned blink:1; /*blink occupies a high byte of the 7th bit */
}ch;
The access of a bit struct member is the same as that of a struct member.
For example, access to the bgcolor member in the previous example bit structure can be written as:
Ch.bgcolor
Attention:
1. Members in a bit structure can be defined as unsigned or signed, but when the member length is 1 o'clock, it is considered to be a unsigned type. Because a single bit cannot have a symbol.
2. Members of a bit structure cannot use arrays and pointers, but bit structure variables can be arrays and pointers, and if they are pointers, their members are accessed in the same way as struct pointers.
3. The total length (number of bits) of the bit structure is the sum of the bits defined by each bit member and can exceed two bytes.
4. Bit structure members can be used with other struct members.
For example:
struct info{
Char Name[8];
int age;
struct addr address;
float pay;
unsigned state:1;
unsigned pay:1;
}workers; '
The structure of the example above defines the information about a worker. There are two bit structure members, each of which has only one byte, so it only takes one bytes and holds two information, the first in that byte indicates the work
Status of the person, and the second indicates whether the pay has been paid. This shows that the use of bit structure can save storage space.
Structural postural structure