C Language Bit field

Source: Internet
Author: User

Original Sticker Address http://www.cnblogs.com/bigrabbit/archive/2012/09/20/2695543.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 struct name {bit field list};

Where the list of bit fields is in the form:

Type descriptor bit domain name : bit field length

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:

Indicates that data is a BS variable, which accounts for two bytes. 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 byte and cannot span two bytes . If one byte 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 b:5/* Start storage from the next unit */    unsigned c:4}

2. Because bit fields are not allowed to span two bytes, the length of a bit field cannot be greater than the length of one byte .

3. 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/* No-bit domain name, the 2-bit cannot use */    int B:3    

Second, the use of bit fields

The following example is to participate in a company (white-collar technology-Qingdao) of the written test encountered, at that time did wrong, in order to fear forget, hurriedly write down.

1 #include <iostream> 2 #include <memory.h> 3 using namespace std; 4 struct A 5 {6     int a:5; 7     int b:3; 8}; 9 int main (void) Ten {one     char str[100] = "0134324324AFSADFSDLFJLSDJF L ";"         a d;13     memcpy (&d, str, sizeof (a));     cout << d.a << endl;15 cout     <& Lt D.B << endl;16     return 0;17}

Output on a 32-bit x86 machine:

$ ./langxun.exe-161

Parsing: By default, in order to facilitate access to and management of elements in the structure, when the structure of the element length is less than the number of bits of the processor, the structure of the longest element inside the unit, that is, the length of the structure must be the longest data element of the integer times If there are elements in the structure with a length greater than the number of processors, the Unit is aligned with the number of bits of the processor. Because it is a 32-bit processor, and the struct A and B element types are int (also 4 bytes), the struct's a consumes 4 bytes of memory.

The above example defines a bit domain structure A, two single-bit fields are a (5 bits occupied), B (occupies 3 bits), so A and b all occupy the structure a one byte (the low one byte).

D memory allocation when the program runs to 14 rows:

High 00110100 00110011   00110001    00110000 Low
' 4 ' 3 ' 1 ' 0 '
where D.A and d.b occupy a byte of D-Low (00110000), d.a:10000, d.b:001

D.A in-memory binary representation is 10000, because D.A is a signed integer variable, the output is to extend the sign bit, so the result is 16 (binary 11111111111111111111111111110000)

D.B in-memory binary representation is 001, because D.B is a signed integer variable, the output is to extend the sign bit, so the result is 1 (binary 00000000000000000000000000000001)

alignment of a bit field

If the struct contains bit fields (Bit-field), then the criteria in VC are:

1) If the adjacent bit field field is of the same type and its bit width is less than the type sizeof size, the subsequent field will be stored next to the previous field until it cannot be accommodated;

2) If the adjacent bit field field is of the same type, but its bit width is greater than the type sizeof size, then the subsequent field will start from the new storage unit, and its offset is an integer multiple of its type size;

3) If the adjacent bit domain field type is different, the specific implementation of the compiler differs, VC6 take the non-compression method (different bit field fields are stored in different bit domain type bytes), dev-c++ and GCC are compressed;

The system first assigns space and padding (padding) to the struct members according to the alignment, and then makes bit-field operations on the variables.

C Language Bit field

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.