The use of middle-level domain in C language

Source: Internet
Author: User
Tags bitwise printf

One, bit field

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

For example:

struct bs
{
int a:8;
int b:2;
int c:6;
};

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:

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:

struct bs
{
unsigned a:4
unsigned :0 /*空域*/
unsigned b:4 /*从下一单元开始存放*/
unsigned c:4
}

In this bit-field definition, A is the first byte of 4 digits, the latter 4 bits are filled with 0, and B starts with the second byte, occupies 4 bits, and C occupies 4 bits.

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, that is, it cannot exceed 8-bit binary.

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:

struct k
{
int a:1
int :2 /*该2位不能使用*/
int b:3
int c:2
};

As can be seen from the above analysis, the bit domain is essentially a type of structure, but its members are distributed by binary.

Second, the use of the bit field

The use of a bit field is the same as that of a struct member, in the general form of a bit-field variable name and a bit domain name field that allows output in various formats.

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);
}

The bit domain structure BS is defined in the previous example program, and three bit fields are a,b,c. Describes the BS type variable bit and pointer variable pbit that points to the BS type. 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 respectively. (Note 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 integer format. Line 13th gives the address of the bit field variable bit to the pointer variable pbit. Line 14th assigns a value to bit field a by pointer, assigned 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 bitwise AND operation result is 3 (111&011=011, decimal value 3). Similarly, the compound bit operation "|=" is used in the 16th line of the program, which is equivalent to: pbit->c=pbit->c|1 the result is 15. The 17th line of the program outputs the values of these three fields by pointer.

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.