Bit segments in C language (bit fields)

Source: Internet
Author: User

A bit segment (Bit-field) defines the space occupied by a member variable in a struct (or union) in bits. A struct (union) that contains a bit segment is called a bit segment structure. The use of bit-segment structure can not only save space, but also easy to operate.

A bit segment is defined in the following format:

Type [var]: Digits

Where type can only be int,unsigned int,signed int three types (int type can not indicate that the negative number depends on the compiler, for example, the VC int is the default is signed int, can represent negative numbers). The bit segment name var is an optional parameter, which can be omitted. Digits represents the number of bits that this bit segment occupies.

I. Use of BIT segments

There are a few points to note when using the bit segments:

1) The type of bit segment can only be int,unsigned int,signed int three type, cannot be char type or floating point type;

2) The bits number of bits should not exceed the maximum number of digits that the base type can represent, such as int is 4 bytes in VC, then it can only be 32 bits;

3) No fame segment can not be accessed, but will occupy space;

4) cannot take the address operation to the bit segment;

5) If the bits number of bits is 0, then this bit segment must be no fame segment, the next segment from the next bit Segment storage unit (here the bit Segment storage unit tested in VC environment is 4 bytes) began to store;

6) If a bit segment appears in an expression, an integer upgrade is automatically made to an int or unsigned int.

7) When assigning a bit segment, it is best not to exceed the maximum range represented by the bit segment, otherwise it may cause unexpected results.

8) The bit segment cannot appear as an array form.

Two. How the bit-segment structure is stored in memory

For bit-segment structures, the compiler automatically optimizes storage space, mainly with these principles:

1) If a bit-segment storage unit can store all the members in the lower segment structure, then all members in the bit-segment structure can only be placed in a single bit-segment storage unit and not in two-bit-segment storage units; If a bit-segment storage unit cannot accommodate all the members in the next segment structure, Then store from the remaining bits from the next bit-segment storage unit. (The size of the bit-segment storage unit in VC is 4 bytes).

2) If there is only a 0-bit nameless segment in a bit structure, only 1 or 0 bytes of space are accounted for (0 bytes in c, and 1 bytes in C + +); otherwise, the space occupied by a bit segment structure is at least the size of a bit segment storage unit;

Bit segments 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.