In operator:

Source: Internet
Author: User


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.


1. Definition of bit fields and description of bit-field variables
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 descriptor 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/* airspace */
Unsigned b:4/* Start storage 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 to use, B starts with the second byte, occupies 4 bits, and 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.
3) Bit fields can have no bit domain name, it is used only 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.
2. 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.
For example:
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 three bit fields (it should be noted that the assignment cannot exceed the allowed range of the 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 bitwise operator "|=" is used in the 16th line of the program, equivalent to:
Pbit-> c=pbit-> c|1
The result is 15. The 17th line of the program outputs the values of these three fields in a pointer manner.


In operator:

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.