Bit field (bit) of C structure body
Some information, when stored, does not need to occupy a full byte, but only a few or a bits. For example, when storing a switch quantity, only 0 and 12 states, with one binary can be. In order to save storage space and make processing simple, C language provides a kind of data structure, called "bit field" or "bit segment". The so-called "bit field" is to divide the binary in a byte into several different regions and describe the number of digits in each region. Each domain has a domain name that allows you to operate by domain name in your program. This allows you to represent several different objects in a single byte bits field.
The definition of a bit field and the description bit field of a bit field variable are similar to the structure definition in the form of:
struct bit domain structure name
{
bit field List
The list of bit fields is in the form:
Type descriptor bit domain name: bit field length
The description of the bit field variable is the same as the description of the structure variable. You can use the first definition, while defining the description or directly describing the three ways. For example:
Copy Code code as follows:
struct BS
{
int a:8;
int b:2;
int c:6;
}data;
Description data is a BS variable, representing two bytes. The bit domain A occupies 8 bits, the bit field B is 2 bits, and the bit domain C occupies 6 bits. There are several instructions for the definition of a bit field:
1. A bit field must be stored in the same byte and cannot span two bytes. If there is not enough space left for one byte to hold another domain, the bit field should be stored from the next unit. You can also intentionally make a bit field start with the next unit. For example:
Copy Code code as follows:
struct BS
{
unsigned a:4
unsigned b:5//* from the next unit start storage * *
unsigned c:4
}
2. Because the bit field is not allowed to span two bytes, the length of the bit field cannot be greater than the length of one byte.
3. A bit field can have a domain name, which is used only for filling or adjusting position. Unknown bit fields are not available. For example:
Copy Code code as follows:
struct k
{
int a:1
Int:2/* No bit domain name, the 2-bit can not be used * *
int B:3
int C:2
};
Second, the use of the bit field
The following example is to participate in a company (white collar Technology-Qingdao) of the written test encountered, at that time did wrong, in order to be afraid of forgetting, hurriedly write down.
Copy Code code as follows:
#include <iostream>
#include <memory.h>
using namespace Std;
struct A
{
int a:5;
int b:3;
};
int main (void)
{
Char str[100] = "0134324324AFSADFSDLFJLSDJFL";
struct A D;
memcpy (&d, str, sizeof (A));
cout << d.a << Endl;
cout << d.b << Endl;
return 0;
}
Output on a 32-bit x86 machine:
Copy Code code as follows:
Parsing: By default, in order to facilitate the access and management of elements in the structure, when the element length of the structure is less than the number of digits of the processor, the longest element in the structure is the unit, that is, the length of the structure must be the integer multiple of the longest data element. If there is an element in the structure with a storage length greater than the number of processor digits, then the number of digits of the processor is aligned to the unit. Because it is a 32-bit processor and the type A and B elements in a struct are int (also 4 bytes), a struct occupies a memory of 4 bytes.
The previous example program defines the bit domain structure A, two single-digit fields is a (occupies 5 bits), B (occupies 3 bits), so A and b all occupy a byte of structure a (low byte).
D memory allocation when the program runs to 14 rows:
Copy Code code as follows:
High 00110100 00110011 00110001 00110000 Low
' 4 ' 3 ' 1 ' 0 '
where D.A and d.b occupy D-Low one byte (00110000), d.a:10000, d.b:001
D.A in memory binary representation is 10000, because D.A is a signed integer variable, the output to the symbol bit extension, so the result is-16 (binary 11111111111111111111111111110000)
D.B in memory binary representation is 001, because D.B is a signed integer variable, the output to the symbol bit extension, so the result is 1 (binary 00000000000000000000000000000001)
Alignment of a bit field
If the structure contains a bit field (Bit-field), then the VC guidelines are:
1 if the adjacent bit field field is of the same type and its bit width is less than the sizeof size of the type, the following field is stored next to the previous field until it cannot be accommodated;
2 If the adjacent bit field field is of the same type, but its bit width is greater than the sizeof size of the type, the subsequent field starts with the new storage cell, with an offset of an integer multiple of its type size;
3 If the adjacent bit field fields are of different types, the specific implementation of each compiler differs, VC6 takes the uncompressed approach (the different bit field fields are stored in different bit-domain type bytes), and dev-c++ and GCC take the compression method;
The system allocates space and padding (padding) to the struct members in alignment, and then carries out bit-field operations on the variables.