C-structured postural domain

Source: Internet
Author: User

bit field:

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. a bit segment member must be declared as an int, unsigned int, or signed int type (short char long).

The definition of a bit field and the description of a bit-field variable are similar to the 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;   
View Code

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. If there is not enough space left in one byte to hold another domain, the bit field should be stored in the next cell. You can also intentionally make a field start from the next unit. For example:

struct BS     {     unsigned A:4     unsigned: 0/* airspace *      / Unsigned b:4/* store starting from the next unit *     /unsigned c:4      }     

In this bit domain definition,a occupies the first byte of 4 bits, the latter 4 bits fill 0 means not used, B starts with the second byte , occupies 4 bits, and C takes 4 bits.

2. the length of the bit field cannot be greater than the length of the data type itself, such as the int type can exceed the 32-bit binary.

3. A bit field can have no bit domain name, it is only used to fill or adjust the location. Nameless bit fields are not available . For example:

struct k     {     int A:1     int :2/* the 2-bit cannot be used *     /int B:3      int c:2     };  

From the above analysis, we can see that the bit field is essentially a type of structure, but its members are assigned by binary.

The use of bit fields in bit fields is the same as the use of the struct members, in the general form of the bit domain variable name. bit domain Name field allows output in various formats.

structBS {unsigned A:1; Unsigned b:3; Unsigned c:4; } bit,*Pbit; BIT.A=1; bit.b=7;//Note: The assignment of a bit field cannot exceed the maximum value that the field can represent, such as B is only 3 bits, the maximum number that can be represented is 7, and if it is assigned 8, an error occurs.Bit.c= the;p rintf ("%d,%d,%d/n", bit.a,bit.b,bit.c);p bit=&Bit;pbit->a=0;p bit->b&=3;p bit->c=1;p rintf ("%d,%d,%d/n", pbit->a,pbit->b,pbit->c);
View Code

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. (Should pay attention to assignment cannot exceed the promised range of this bit field) program line 12th 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 16th line of the program uses the compound bit operation "=", equivalent to: pbit->c=pbit->c1 the result is 15. The 17th line of the program outputs the values of these three fields in a pointer manner.

Let's take a look at the following two structure 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 out the size of the two structures, and we get the result:
sizeof ( struct Foo2) = 1
sizeof (struct Foo3) = 2
> Obviously not what we were expecting, if the two structures should be 3 in size according to normal memory alignment rules, then what's the problem? The first thing we can be sure of is that a struct with a ' bit field ' is not aligned to each domain, but rather that some bit domain members are ' bundled ' together to align. In Foo2, for example, all members of this struct are of type char, and the total space occupied by the three bit fields is 6 bit < 8 bit (1 byte), when the compiler aligns the three members together, and at the cost of minimal space, That's why we get the result of sizeof (struct foo2) = 1. And look at the FOO3 structure, like Foo2, three member types are also char type, but three member bit domain occupies 9 bit > 8 bit (1 byte), where the bit domain cannot span two member basic type space, then the compiler will be a and B two members ' bundle ' aligned by Char, and C is individually aligned with the char type, so there is actually a gap between B and C, but this is also the most space-saving method. Let's look at one more structure definition:

struct Foo4 {char    2 ; Char    3 ; int 1 ;};

In Foo4, although the sum of space occupied by three bit fields is 6 bit < 8 bit (1 byte), because the alignment coefficients of char and int are different, they cannot be bundled together, is it not a, B bundled together according to Char, c is aligned by int alone? We print the sizeof (struct Foo4) discovery result is 8, which means the compiler binds a, B, c together and aligns it with Int. That is , if there is not enough one type of size, it will be aligned to the largest of those types. This is aligned by int.


C99 specifies that int, unsigned int, and bool can be used as bit field types, but almost all compilers extend this.
Allows the existence of other type types.
The primary purpose of using bit fields is to compress the storage, with the following approximate rules:

if the adjacent bit field field is of the same type, but its bit width is greater than the type sizeof size, then the subsequent field will start from the new storage unit, and its offset is an integer multiple of its type size; If the bit field fields are interspersed with non-bit field fields, they are not compressed;5) The total size of the entire struct is an integer multiple of the size of the widest base type member.
structS1 {intI8; intJ:4; intA:3; Doubleb;}; structS2 {intI8; intJ:4; Doubleb;intA:3; }; printf ("sizeof (S1) =%d/n",sizeof(S1)); printf ("sizeof (s2) =%d/n",sizeof(S2)); Result: -, - 
View Code


In the first struct, the i,j,a is a total of 15 bits, less than 8 bytes, and is aligned in double 8 byte, a total of 16 bytes

In the second struct, i,j a total of 12 bits, less than 8 bytes, 8-byte alignment, a also by 8-byte alignment, plus double 8+8+8=24 bytes

C-structured postural domain

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.