A brief analysis of C language bit domain and bit segment

  • 2020-04-02 01:16:27
  • OfStack

C potential domain of structure (bit segment)
Some information is stored not in a single byte, but in a few or one binary bits. For example, when storing a switch quantity, there are only two states: 0 and 1. To save storage space and make processing easier, C provides another data structure called a "bit field" or "bit segment." A "bit field" is the division of the binary bits of a byte into different regions and the number of bits in each region. Each domain has a domain name, allowing you to work by domain name in the program. This allows you to represent several different objects in a binary bit field of one byte.

1. The definition of bit-field and the explanatory bit-field definition of bit-field variables are similar to the structure definition in the form of:
Struct bit domain name
{
A list of domain
}; Where the form of the list of bit fields is:

Type descriptor bit domain: bit field length
Bit-domain variables are described in the same way as structural variables. Can be used after the definition of the definition, at the same time or directly explain the three ways. Such as:


struct bs
{
  int a:8;
  int b:2;
  int c:6;
}data; 

Data as bs variable, a total of two bytes. The bit domain a occupies 8 bits, bit domain b 2 bits and bit domain c 6 bits. For the definition of bit field, there are the following points:


1. A bit field must be stored in the same byte, not across two bytes. If one byte does not have enough space for another field, the bit field should be stored from the next cell. You can also intentionally start a domain from the next cell. Such as:


struct bs
{
    unsigned a:4
    unsigned b:5 
    unsigned c:4
}

2. Since bitfields are not allowed to span two bytes, the length of a bitfield cannot be greater than one byte.

3. A bit-domain can have no bit-domain name, in which case it is used only for padding or positioning. Unknown bit fields cannot be used. Such as:


struct k
{
    int a:1
    int :2 
    int b:3
    int c:2
}; 

Two, the use of bit fields
The following is an example of taking the written test of a company (white-collar technology - Qingdao).

#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 << d.a << endl;
     cout << d.b << endl;
     return 0;
 }

Output on 32-bit x86 machines:

?
$ ./langxun.exe
-16
1 

Analysis: by default, in order to facilitate the access and management of the elements in the structure, when the length of the elements in the structure are less than the number of bits of the processor, the longest element in the structure is taken as its unit, that is, the length of the structure must be an integer multiple of the longest data element. If a structure contains elements whose length is greater than the number of processor bits, the number of processor bits is used as the alignment unit. Since it is a 32-bit processor, and both a and b elements in the structure are of type int (also 4 bytes), a of the structure takes up 4 bytes of memory.

In the above program, we defined the bit domain structure A, the two bits fields are A (occupying 5 bits) and b (occupying 3 bits), so A and b occupy A byte of structure A (one byte in low order).

When the program runs to 14 lines, d memory allocation:


  high  00110100 00110011   00110001    00110000  low 
       '4'       '3'       '1'          '0'  
  Among them d.a and d.b Take up d Low byte ( 00110000 ) ,d.a : 10000, d.b : 001

  Da memory binary representation of 10000, as a result of da as the signed integer variables, extensions to the output of the sign bit, so the result is - 16 (binary 11111111111111111111111111110000)

  Db in memory binary representation of 001, due to the db for a signed integer variables, extensions to the output of the sign bit, so the result is 1 (binary 00000000000000000000000000000001)

Alignment of bit fields
If the structure contains bit-field, then the criterion in VC is:

1) if the fields of adjacent bit fields have the same type, and the sum of their bit widths is less than sizeof of type, then the fields of the latter will be stored next to the previous field until they cannot be contained;

2) if adjacent bit field fields have the same type, but the sum of their bit widths is greater than sizeof sizeof the type, then the following fields will start from a new storage cell with an offset of integer times the sizeof their type;

3) if the types of adjacent bit-field fields are different, the specific implementation of each compiler is different. VC6 adopts the uncompressed mode (different bit-field fields are stored in different bit-field type bytes), dev-c ++ and GCC adopt the compressed mode.

The system allocates space and padding to the members of the structure, and then performs bit-field operations on the variables.


Related articles: