Example and explanation of C-median domain combination alignment)

Source: Internet
Author: User
Tags domain list

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, we can use a byte binary for several different objects.
The bitwise 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. Because the bit field cannot span two bytes, the length of the bit field cannot exceed the length of one byte, that is, it cannot exceed 8-bit binary.
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.

If the struct contains bit-field, the guidelines in VC are:
1) if the types of adjacent fields are the same, and the sum of the bit widths is smaller than the sizeof size of the type, the subsequent fields will be stored next to the previous field until they cannot be accommodated;
2) If the Field Types of adjacent bit fields are the same, but the sum of Bit Width is greater than the sizeof size of the type, the subsequent fields start from the new storage unit, its offset is an integer multiple of its type;
3) if the types of adjacent bitfield fields are different, the specific implementation of each compiler varies, vc6 adopts the non-compression mode (the fields of different bit domains are stored in different bit domain type bytes), and both Dev-C ++ and GCC adopt the compression mode;

The system first allocates space and padding for the struct members according to the alignment, and then performs bitfield operations on the variables.

Do you understand? Try the following examples (vs 2003 ).

# Pragma pack (1)
Struct S4 {
Char A: 4;
Short B: 4;
Short C: 4;
Long D;
};

Output S4 sizeof: 7

# Pragma pack (1)
Struct S4 {
Char A: 4;
Short B: 4;
Char C: 4;
Long D;
};

Output S4 sizeof: 8

# Pragma pack (2)
Struct S4 {
Char A: 4;
Short B: 4;
Char C: 4;
Long D;
};

Output S4 sizeof: 10

# Pragma pack (2)
Struct S4 {
Char A: 4;
Short B: 4;
Short C: 4;
Long D;
};

Output S4 sizeof: 8

# Pragma pack (2)
Struct S4 {
Char A: 4;
Short B: 4;
Short C: 7;
Long D;
};

Output S4 sizeof: 8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.