Bit field of struct in C language (Bit-fields)

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_6240b5980100tcba.html

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.

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;
}da ta;
description data is a BS variable, accounting for two bytes (this assumes that the int type is 16 bits long, and that int is usually 32 bits). 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 cell and cannot span two cells. If one unit 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
unsigned b:4
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. 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
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.
in short, words and short
This is the representation of the bit domain operation, which means that the following ": 1" means that the size of the member is 1 bits of the defined type, ": 2" occupies 2 bits, and so on. Of course, the size cannot exceed the total number of bits that the defined type contains.
a bytes (byte) is a 8 bit (bit). For example, the type defined in your structure is U_char, a byte, a total of 8bit, and the maximum cannot exceed 8.
32-bit machine,
Short is 2 bytes, a total of 16bit, the largest can not exceed.
int is 4 bytes, total 32bit, maximum cannot exceed.
In turn.
this defines a more space-saving definition. For example, the structure above you, the variable type defined is U_char, is a byte type, that is, 8bit.
Fc_subtype accounted for the 4bit,fc_type accounted for 2bit,fc_protocol_version accounted for 2bit, a total of 8bit, exactly a byte.
The other eight members, each accounted for 1bit, a total of 8bit, exactly is a byte.
Therefore, if the size of your structure is calculated using sizeof (struct Frame_control), it is 2bytes.

Bit fields for structs in C language (Bit-fields)

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.