While some information is stored, it does not need to occupy a full byte, only a few or one bits. For example, when storing a switching volume, there are only 0 and 12 states, with one binary. In order to save storage space and make processing simple, C language also provides a data structure, called "bit field" or "bit segment". The so-called "bit field" is to divide the binary in one byte into several different regions and describe the number of bits per region. Each domain has a domain name that allows operations to be performed by domain name in the program. This allows you to represent several different objects in a bits field of one byte.
The definition of a bit field and the description of a bit field variable
a bit field definition is similar to a structure definition in the form of:
struct bit domain structure name
{bit field list};
where the list of bit fields is in the form: type specifier bit domain name: bit field length
For example:
struct BS
{
int a:8;
int b:2;
int c:6;
};
the description of a bit-field variable is the same as the structure variable description. Can be defined by the first description, at the same time define the description or direct description of the three ways. For example:
struct BS
{
int a:8;
int b:2;
int c:6;
}data;
indicates that data is a BS variable, which accounts for two bytes. Where bit domain A occupies 8 bits, bit domain B is 2 bits, bit field C is 6 bits. There are several explanations for the definition of bit fields:
1. a bit field must be stored in the same byte and cannot span two bytes. If one byte has enough space left to hold another domain, the bit field should be stored from the next cell. You can also intentionally make a field start from the next unit.
For example:
struct BS
{
unsigned a:4;
unsigned:0;
unsigned b:4;
unsigned c:4;
}
in this bit domain definition, a occupies 4 bits of the first byte, and the next 4 bits fill 0 to not use, B starts with the second byte, occupies
4 bits, C occupies 4 bits.
2. because bit fields are not allowed to span two bytes, the length of the bit field cannot be greater than the length of one byte, that is, it cannot exceed the 8-bit binary.
second, the use of bit fields
bit fields are used in the same way as struct members, in the general form of:
bit domain variable name • Bit domain name
bit fields allow output in a variety of 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 example program, the bit-domain structure BS is defined, and the three-bit domain is a,b,c. The variable bit for the BS type and pointer variable pbit to the BS type are described. This means that a bit field can also use pointers. The 9, 10, and 113 lines of the program assign values to the three bit fields respectively. (Note that the assignment cannot exceed the allowed range of this bit field) the 12th line of the program outputs the contents of three fields in an integer format. The 13th line sends the address of bit field variable bit to pointer variable pbit. The 14th row assigns a value to bit field A as a pointer to 0. Line 15th uses the compound bitwise operator "&=", which is equivalent to: pbit->b=pbit->b&3 bit field B has a value of 7, and 3 for bitwise AND operation results of 3 (111&011=011, decimal value 3). Similarly, the compound bit operation "|=" is used in the 16th line of the program. The reason to have a basic knowledge of dialysis such a column, is to tell everyone in detail the truth, rough things who all understand, cultivation of internal strength for the master of the way.
there is a flaw in the previous content, but also refer to the following article:
C99 specifies that int, unsigned int, and bool can be used as bit field types, but almost all compilers extend this to allow the existence of other type types.
The primary purpose of using bit fields is to compress the storage, with the following approximate rules:
1) If the adjacent bit field field is of the same type and its bit width is less than the type sizeof size, then the following word
the segment will be stored adjacent to the previous field until it cannot be accommodated;
2) If the neighboring bit field field has the same type, but its bit width is greater than the type sizeof size, then the following word
The segment begins with the new storage unit, and its offset is an integer multiple of its type size;
3) If the adjacent bit field fields are of different types, the specific implementations of each compiler differ, VC6 take the uncompressed side
, dev-c++ adopts compression method;
4) If the bit field fields are interspersed with non-bit field fields, no compression is performed;
5) The total size of the entire struct is an integer multiple of the size of the widest base type member.
C Language Bit field