[Advanced C Language: Detailed Explanation of Custom Types] Bit segment

Highlights of this section: 

  • what is bit segment
  • bit segment memory allocation
  • Cross-platform issues with bit segments
  • application of bits

⚡What is bit segment

Bit field declarations and structures are very similar, with two differences:

  1. Members of bit fields must be int, unsigned int, or signed int.
  2. A colon and a number follow the member name of a bit field.
struct A
{
    int _a:2;
    int _b:5;
    int _c:10;
    int _d:30;
};

The code A above is a bit segment type. What is the size of segment A? Let's go ahead and test it out:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

struct A
{
    int _a : 2;
    int _b : 5;
    int _c : 10;
    int _d : 30;
};

int main()
{
    printf("%d\n", sizeof(struct A));
    return 0;
}

The result of running the code is as follows:

 This may be somewhat different from the 16 that everyone expected, so if we want to really understand the bit segment, we must learn the distribution rules of the bit segment in memory.


⚡ Memory allocation for bit segments

  • Members of a bit field can be of type int, unsigned int, signed int, or char (belonging to the integer family).
  • The space of the bit field is opened up in the form of 4 bytes (int) or 1 byte (char) according to the need.
  • Bit segments involve many uncertain factors, bit segments are not cross-platform, and programs that focus on portability should avoid using bit segments.
举个例子:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

struct S
{
    char a : 3;
    char b : 4;
    char c : 5;
    char d : 4;
};


int main()
{ 
    struct S s = { 0 };
    s.a = 10;
    s.b = 12;
    s.c = 3;
    s.d = 4;
    return 0;
}

 In the VS environment, the allocation of the bit segment of s in memory can be obtained through debugging:

 Mind Diagram:


⚡Cross-platform issues with bit segments

  1. It is undefined whether an int bit field is treated as signed or unsigned.
  2. The maximum number of bits in a bit field cannot be determined. (16-bit machines can be up to 16, 32-bit machines can be up to 32, written as 27, there will be problems on 16-bit machines.
  3. Whether members of a bit field are allocated from left to right in memory or from right to left is undefined.
  4. When a structure contains two bit fields, and the members of the second bit field are too large to accommodate the remaining bits of the first bit field, it is uncertain whether to discard or utilize the remaining bits.

Therefore, it can be concluded that compared with the structure, the bit segment can achieve the same effect, but it can save space very well, but there are cross-platform problems.


⚡Application of bit segments

Format encapsulation of data transmission:

If you use int for every part of the data packet, even char will cause a very large waste of space. So here we can save space through bit segments, so that each part occupies as little space as possible, so that the size of the entire data packet becomes smaller.

Think of the network as a data highway. If the data is large, no matter how fast a small data packet is, it cannot pass over the data packets that are larger than it before it, and can only be transmitted one by one in order. . If the data packet can be made as small as possible, more data will be transmitted per unit time on the entire network, and the speed will be correspondingly accelerated. Smaller means faster.


 Thank you for reading this blog. It took a long time to create it. Friends think my blog is helpful to you. You may wish to leave your likes and favorites, follow me, and show you a different C language. 

98b76a6f4a9c4ca88fd93da1188ac6f9.gif

Guess you like

Origin blog.csdn.net/JX_BC/article/details/129950114