C language bit domain usage and precautions

Source: Internet
Author: User

The so-called "bit domain" refers to dividing the binary in a byte into several different regions,
The number of digits in each region. Each domain has a domain name, which allows operations by domain name in the program.
In this way, several different objects can be represented by a byte binary field. The definitions of bit domains and bit domain variables are similar to those of structure definitions, in the form:

Struct bit domain structure name

{
Bit domain list };

Eg: struct weiyu {


Int A: 2;


Int B: 5;


INT: 5; // these five digits are in airspace and cannot be used.


Int C: 3;


}

Although bit fields are easy to use, note the following when using them:

1) if the type of the adjacent bit field is the same and the sum of its bit width is smaller than the sizeof size of the type

Segments are stored next to the previous field until they cannot be accommodated;

2) If the field type of the adjacent bit field is the same, but the sum of its bit width is greater than the sizeof size of the type

The offset from the new storage unit is an integer multiple of its type;

3) the total size of the entire struct is an integer multiple of the size of the widest basic type.

4) if the types of adjacent bitfield fields are different, the specific implementations of each compiler are different.

Type, Dev-C ++ adopts the compression method;

5) if the bit field is interspersed with non-bit field, no compression is performed. (Not applicable to all compilers)

4, 5 has a big relationship with the compiler, so be careful when using it and avoid it as much as possible.

# Include "stdio. H"

Void main (INT argn, char * argv)

{

Struct
Test {

Unsigned A: 10;

Unsigned B: 10;

Unsigned C: 6;

Unsigned: 2; // This two bytes can't use

Unsigned D: 4;

} Data, * pdata;

Data. A = 0x177;

Data. B = 0x111;

Data. c = 0x7;

Data. D = 0x8;

Pdata = & data;

Printf ("data. A = % x Data. B = % x Data. C = % x Data. D = % XN ", pdata-> A, pdata-> B, pdata-> C, pdata-> D); // you can use pointers for bit fields.

Printf ("sizeof (data) = % DN", sizeof (data ));
// 4 bytes, the most common case

Struct testlen {

Char A: 5;

Char B: 5;

Char C: 5;

Char D: 5;

Char E: 5;

} Len;

Printf ("sizeof (LEN) = % DN", sizeof (LEN ));
// 5 bytes Rule 2

Struct testlen1 {

Char A: 5;

Char B: 2;

Char D: 3;

Char C: 2;

Char E: 7;

} Len1;

Printf ("sizeof (len1) = % DN", sizeof (len1 ));
// 3 bytes Rule 1

Struct testlen2 {

Char A: 2;

CHAR: 3;

Char B: 7;

Long D: 20; // 4 bytes

Char E: 4;

} Len2;

Printf ("sizeof (len2) = % DN", sizeof (len2 ));
// 12 rule 3, 4, 5, the total length is an integer multiple of 4, 2 + 3
1 byte, B 1bye
2 + 3 + 7
4 bytes, followed by d
Optimized with E
Occupies 4 bytes

Struct testlen3 {

Char A: 2;

CHAR: 3;

Char B: 7;

Long D: 30;

Char E: 4;

} Len3;

Printf ("sizeof (len3) = % DN", sizeof (len3); // 12
Rule 3, 4, 5, the total length is an integer multiple of 4, 2 + 3
1 byte, B 1bye
2 + 3 + 7
4 bytes, and D occupies 4 bytes. To ensure that long occupies 4 bytes of E

}

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.