C Language Learning Tutorial eighth chapter-Enumeration, bitwise operations (4)

Source: Internet
Author: User
Tags definition bitwise

Bit fields

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/* Airspace * *
unsigned b:4//* from the next unit start storage * *
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/* The 2-bit can not be used * *
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.

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.