C structure Position Domain, Position Domain

Source: Internet
Author: User
Tags domain list

C structure Position Domain, Position Domain

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.The bits must be declared as int, unsigned int, or signed int (short char long ).

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;View Code

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. 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/* 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 fill in 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 the bit field cannot exceed the length of the data type. For example, the int type can exceed 32-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 use */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.

2. The use of bit domains is the same as that of structure members. The form is generally: Bit domain variable name. Bit domain name bit domains can be output in various formats.

 

Struct bs {unsigned a: 1; unsigned B: 3; unsigned c: 4;} bit, * pbit; bit. a = 1; bit. B = 7; // Note: The value assignment of a bit field cannot exceed the maximum value that can be expressed by this field. For example, if B has only three bits, the maximum value that can be expressed is 7. If B is assigned 8, the error bit will occur. 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 );View Code

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. (It should be noted that the value assignment cannot exceed the promised range of the bit field) the program's 12th rows output the content of the three fields in integer format. 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 compound bitwise operation "=" is used in Row 3 of the program, which is equivalent to pbit-> c = pbit-> c1, and the result is 15. The program output the values of the three fields in the pointer mode in Row 3.

Let's take a look at the following two struct definitions:

struct foo2 {char    a : 2;char    b : 3;char    c : 1;};struct foo3 {char    a : 2;char    b : 3;char    c : 7;};

Let's print the size of the two struct. The result is:
Sizeof (struct foo2) = 1
Sizeof (struct foo3) = 2
Obviously we didn't expect it. If the two struct sizes should be 3 in accordance with the normal memory alignment rules, what is the problem? First of all, we can be certain through this phenomenon that the struct with a 'bit region' is not aligned according to each domain, but alignment is made by bundling some bit domain members together. Taking foo2 as an example, all the members of this struct are of the char type, and the total space occupied by the three bit fields is 6 bit <8 bit (1 byte ), in this case, the compiler will bind these three members together for alignment at the minimum space cost, which is why we get sizeof (struct foo2) = 1. Let's look at the structure foo3. Like foo2, the three member types are also char type, but the space occupied by the three member fields is 9 bit> 8 bit (1 byte ), here, the bitfield cannot span the basic type space of two members. In this case, the compiler binds the members a and B to align according to char, while c performs alignment separately based on char, in this way, there is actually a gap between B and c, but this is also the most space-saving method. Let's look at another struct definition:

struct foo4 {char    a : 2;char    b : 3;int c : 1;};

In foo4, although the space occupied by the three single-digit fields is 6 bit <8 bit (1 byte), the alignment coefficients of char and int are different and cannot be bundled together, isn't a and B bundled together aligned by char, and c aligned by int separately? We printed the sizeof (struct foo4) and found that the result is 8. That is to say, the compiler binds a, B, and c together and uses int as alignment. That is to sayWhen the size of one type is not enough, it is aligned according to the largest type.This is aligned by int.


C99 specifies that int, unsigned int, and bool can be bit domain types, but almost all compilers have extended this,
Other types are allowed.
The main purpose of bit domains is to compress storage. The general rules are as follows:

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 are stored next to the previous field until they cannot be accommodated. 2) if the 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 bit field are different, the implementation of each compiler varies. VC6 adopts the non-compression mode, dev-C ++, GCC adopts the compression method; 4) if the bit field is interspersed with a non-bit field, it is not compressed; 5) the total size of the entire struct is an integer multiple of the size of the widest basic type member.
Struct s1 {int I: 8; int j: 4; int a: 3; double B ;}; struct s2 {int I: 8; int j: 4; double B; int a: 3 ;}; printf ("sizeof (s1) = % d/n", sizeof (s1); printf ("sizeof (s2) = % d/n ", sizeof (s2); result: 16, 24View Code


In the first struct, I, j, and a occupy 15 digits, less than 8 bytes, aligned by double 8 bytes, 16 bytes in total

In the second struct, I and j occupy 12 digits, less than 8 bytes. They are aligned by 8 bytes. a is also aligned by 8 bytes, and double is 8 + 8 + 8 = 24 bytes.

 

Related Article

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.