A detailed description of the C-language bit segment

Source: Internet
Author: User
Bit segments in C language





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.



Then defining a bit segment structure can be defined as the following code:


struct node
{unsigned int a: 4; // bit segment a, occupying 4 bits
     unsigned int: 0; // unnamed bit segment, occupying 0 bits
     unsigned int b: 4; // bit segment b, occupying 4 bits
     int c: 32; // bit segment c, occupying 32 bits
     int: 6; // Unnamed segment, occupying 6 bits
     };


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;



Test procedure:


/ * Test bit 201110.12 * /
#include <iostream>
using namespace std;
typedef struct node
{
unsigned int a: 1; // There is a non-zero bit segment, which occupies at least 4Byte
} S;
typedef struct node1 // occupies 1 byte of space in C ++ and 0 bytes in C
{
unsigned int: 0;
} S1;
typedef struct node2
{
unsigned int a: 1;
unsigned int: 0; // The next bit segment is placed in a new bit segment storage unit, so it takes 4 + 4 = 8Byte
unsigned c: 32;
} S2;
typedef struct node3
{
 unsigned int a: 4;
 unsigned int: 0;
 int: 6; // This bit segment is placed in a new bit segment storage unit
 unsigned c: 32; // Because 6 + 32> 32, the segment c is also placed in a new bit segment storage unit, so it takes 4 + 4 + 4 = 12Byte
} S3;
typedef struct node4
{
unsigned int a: 1;
char b; // All members can be stored in a bit segment storage unit, so it takes 4Byte
int c: 1; int d: 2; unsigned int e: 2;
} S4;
nt main (int argc, char * argv [])
{
S4 s4;
s4.a = 1;
s4.c = 1;
s4.d = 2;
s4.e = 3;
printf ("% d% d% d% d \ n", s4.a, s4.c, s4.d, s4.e);
printf ("% d% d% d% d% d \ n", sizeof (S), sizeof (S1), sizeof (S2), sizeof (S3), sizeof (S4));
return 0;
}


The result of the execution is:



1-1-2 3
4 1 8) 12 4
Please press any key to continue ...
When you print individual bits of a S4, the result is different from the initial value assigned.



Since c accounts for only 1 bits, there is no data bit, at which point the symbol extension is added 1 directly at the high, so the result of printing is-1;



Since D occupies 2 bits, when the 2 is assigned to D, the contents in memory are 10, the symbol is extended at this time, the high 1 is 0XFF FF FF FE, then the true value is-2.



Thank you for reading, hope to help everyone, thank you for the support of this site!

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.