c struct bit field (bit segment)

Source: Internet
Author: User

The bit field of C struct (bit segment) Some information is stored without having to occupy a full byte, which only takes up 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:structbit domain struct name {bit field list}; The list of bit fields is in the form of type specifier bit domain name: The description of the bit field length-domain 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:structbs{intA:8; intB:2; intC: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:structbs{unsigned A:4unsigned 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 a single byte . 3. a bit field can have no bit domain name, and it is used only to fill or adjust the position . Nameless bit fields are not available. For example:structk{intA:1    int:2 /*no bit domain name, the 2-bit cannot be used*/    intB:3    intC:2}; Second, bit field use the following example is to join a company (white-collar technology-Qingdao) The written test encountered, at that time did wrong, in order to fear forget, hurriedly write down. 

#include <iostream>
#include <memory.h>
using namespace Std;
struct A
{
int a:5;
int b:3;
};
int main (void)
{
Char str[100] = "0134324324AFSADFSDLFJLSDJFL";
struct A D;
memcpy (&d, str, sizeof (A));
cout << sizeof (A) << Endl;
cout << d.a << Endl;
cout << d.b << Endl;
return 0;
}

Output on a 32-bit x86 machine:

$ ./Langxun.exe- -1parsing: 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: High when the program runs to 14 rows00110100 00110011   00110001    00110000Low'4'       '3'       '1'          '0'where D.A and d.b occupy a byte of D-Low (00110000), D.A:10000, D.B:001d.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- -(binary is 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) Three, bit field alignment if the struct contains bit fields (bit -field), then the guidelines in VC are:1if 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;2if the adjacent bit field field has the same type, but its bit width is greater than the type sizeof size, then the subsequent field will start from the new storage cell, with an offset of an integer multiple of its type size;3If the adjacent bit field fields are of different types, the specific implementation of each compiler differs, VC6 takes an uncompressed approach (different bit field fields are stored in different bit-field type bytes), dev-c++and GCC are compressed; the system assigns space and padding (padding) to the struct members by alignment, and then the variable is bit-field-scoped. 

c struct bit field (bit segment)

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.