c struct bit field (bit segment)

Source: Internet
Author: User

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

A bit field definition is similar to a 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:

 struct  bs{  int A:8;   int B:2;  int c:6;} data;

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 be used */    int B:3    int c:2};

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.

#include <iostream>#include<memory.h>using namespacestd;structa{intA:5; intB:3;};intMainvoid){    Charstr[ -] ="0134324324AFSADFSDLFJLSDJFL"; structA D; memcpy (&d, str,sizeof(A)); cout<< D.A <<Endl; cout<< d.b <<Endl; return 0;}

Output on a 32-bit x86 machine:

$./Langxun.exe-1

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  00110000   low   " 4   " "  3   " "  1   " "  0   "  where D.A and d.b occupy D-Low one byte ( 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.

I also wrote a simple code test a bit,

#include"stdafx.h"structch1{intA:1; intB:2; intC:5;} data1;structch2{CharA:8; unsignedCharB:7; CharC:6; CharD:3; CharE:2; unsignedCharF:1; intG;}  Data2; //do not add a bit field to occupy 12 bytesstructch3{CharA; Charb; CharC; CharD; Chare; CharF; intG;} Data3;intMainintargcChar*argv[]) {DATA2.F=0x3; data2.b=0xFF; printf ("Hello world!\n"); printf ("size of data1=%d data2=%d data3=%d\n",sizeof(DATA1),sizeof(DATA2),sizeof(DATA3)); printf ("data2.f=%x,data2.b=%x\n", data2.f,data2.b); return 0;}

Printing results,

Hello world! size of Data1=4 data2=8 data3=data2.f=1, data2.b=7f

c struct bit field (bit segment)

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.