Discussion on bit segments in C Language

Source: Internet
Author: User

 

Bit-field defines the space occupied by member variables in a struct (or consortium) in units of bits. A struct (Consortium) with a bit segment is called a bit segment structure. The bit segment structure can save space and facilitate operations.

Bit segment Definition Format:

Type [var]: digits

The value of type can only be int, unsigned int, or signed int (whether the int type can represent a negative number depends on the compiler. For example, in VC, the int type defaults to signed int and can represent a negative number ). The bit segment name var is an optional parameter, which can be omitted. Digits indicates the binary digits occupied by this bit segment.

The following code defines the structure of a single segment:

Struct node

{

Unsigned int a: 4; // segment a, which occupies 4 digits

Unsigned int: 0; // unsigned segment, which occupies 0 digits

Unsigned int B: 4; // segment B, which occupies 4 digits

Int c: 32; // segment c, which occupies 32 digits

Int: 6; // The unsung segment, which occupies 6 digits.

};

I. Use of bit segments

Pay attention to the following points when using bit segments:

1) Bit segments can only be int, unsigned int, or signed int types. They cannot be char or floating point;

2) the binary digits of a bit segment cannot exceed the maximum digits of the basic type. For example, if int occupies 4 bytes in VC, it can only be 32 characters at most;

3) Anonymous segments cannot be accessed, but occupy space;

4) The bitwise segment cannot be used for address fetch;

(5) If the binary number of a bit segment is 0, the bit segment must be an unknown segment, the next bit segment is stored from the next bit Block Storage unit (the bit block storage unit is tested to be 4 bytes in the VC environment;

6) if the bit segment appears in the expression, the integer is automatically upgraded and automatically converted to int or unsigned int.

7) when assigning values to a position segment, it is best not to exceed the maximum range that can be expressed by the bit segment; otherwise, unexpected results may occur.

8) Bit segments cannot appear in the form of arrays.

Ii. Storage Method of bit segment structure in memory

For the bit segment structure, the compiler will automatically optimize the storage space. The main principles are as follows:

1) if a single-Segment Storage unit can store all the members in the structure of the next-segment, all the Members in the structure can only be stored in one single-segment storage unit, it cannot be placed in two block storage units. If a block storage unit cannot accommodate all the members in the structure of the lower segment, the remaining segments are stored from the next block storage unit. (The size of the storage unit in the VC middle segment is 4 bytes ).

2) If there is only one anonymous segment with 0 digits in a single segment structure, it only occupies 1 or 0 bytes of space (in C, it occupies 0 bytes, C ++ occupies 1 byte); otherwise, the space occupied by a single segment structure is at least the size of a single segment storage unit in any other circumstances;

Test procedure:

/* Test Bit section 201110.12 */

# Include <iostream>

Using namespace std;

 

Typedef struct node

{

Unsigned int a: 1; // a non-zero bit segment occupies at least 4 bytes.

} S;

 

Typedef struct node1 // occupies 1 byte space in C ++, and 0 byte in C

{

Unsigned int: 0;

} S1;

 

Typedef struct node2

{

Unsigned int a: 1;

Unsigned int: 0; // put the next bit segment in a new bit block storage unit, so it occupies 4 + 4 = 8 bytes

Unsigned c: 32;

} S2;

 

Typedef struct node3

{

Unsigned int a: 4;

Unsigned int: 0;

Int: 6; // put this bit block in a new bit Block Storage Unit

Unsigned c: 32; // because of 6 + 32> 32, the bit c is also placed in a new bit block storage unit, so it occupies 4 + 4 + 4 = 12 Byte

} S3;

 

Typedef struct node4

{

Unsigned int a: 1;

Char B; // all members can be stored in a single-bit storage unit, so it occupies 4 bytes.

Int c: 1;

Int d: 2;

Unsigned int e: 2;

} S4;

 

 

Int main (int argc, char * argv [])

{

S4;

S4.a = 1;

S4.c = 1;

S4.d = 2;

S4.e = 3;

Printf ("% d \ n", s4.a, s4.c, s4.d, s4.e );

Printf ("% d \ n", sizeof (S), sizeof (S1), sizeof (S2), sizeof (S3 ), sizeof (S4 ));

Return 0;

}

 

The execution result is:

1-1-2 3

4 1 8 12 4

Press any key to continue...

When printing each bit segment of s4, the printed result is different from the assigned initial value.

Since c only occupies 1 digit, there is no data bit. In this case, the symbol extension is directly added to 1 at the high position, so the printed result is-1;

Because d occupies 2 places, when 2 is assigned to d, the content stored in the memory is 10. In this case, the symbol is extended and the value 0 x ff fe is used when 1 is supplemented at a high level, then its true value is-2.

 

Author Hai Zi

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.