In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how the C language implements the bit mechanism". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "how to realize the bit mechanism in C language".
Concept
What is a bit?
Bit bit field is also called bit field. C language allows you to specify the memory length of its members in bits in a structure, which is called bit segment. The use of bit segments can save space by storing data with fewer digits.
The bit segment is unique to the structure, so the declaration is similar to the structure, but there are two differences:
1. Segment members must be int, unsigned int,signed int
two。 The structure after the segment member name includes a colon and an integer number.
Take a chestnut:
Struct {unsigned int ch: 8; / / 8-bit unsigned int chh: 6; / / 6-bit unsigned int chhh: 18; / / 18 bit}; struct dest; memory allocation
The here is a bit type, this mysterious bit we are not common, so how to calculate its size? It's actually the same as the structure:
Struct arr {int alux 2 ntint blun 5 ntint cpule 10 sectint dapplo 30;}; int main () {printf ("% d\ n", sizeof (struct arr)); return 0;}
What is the meaning of aV2 here?
Member an only occupies 2 bits, which is the same later. In that case, it is enough for us to total 47 bits, that is, 6 bytes and 48 bits, so why 8? What exactly is the memory allocation of the bit segment?
Here, do not make a low-level mistake to think that his size is 47 bits, which is the same as the principle described in the previous blog post, because all members are int type, all members will be the same as int, int is four bytes, the default is 8 bytes, the alignment number is 4 bytes, Ababb < 1 byte, merge application 1 byte space, then all need independent application space, a total of 1 / 2 bytes / 4 = 7 bytes, the size of the structure must be an integer multiple of the maximum alignment An integer multiple of 4 is 8, so it is 8 bytes.
Cross-platform problem of bit segment
Let's think about this mechanism. Arecronomy bjorcred is respectively 2Magne5 and 10je 30 bits, while my byte is 8 bits. If there are 6 bits left after allocating 1 byte to a, should I give these six seats to the second brother b members to enjoy? Do I drain capital byte by byte or spend a lot of money to arrange a "single room"? There is ambiguity here.
To know that bit fields open up space in terms of four-byte int or one-byte char as needed, it involves a lot of uncertainties, which is why bit fields are not cross-platform, and programs that pay attention to portability should avoid using them.
Is there any uncertainty, such as the memory allocation problem we just mentioned, this problem does not even specify how I should use the C language standard, which needs to be determined by the specific compiler environment, which depends on different platforms such as Linux is the gcc standard and VS is the windows standard.
We need to know:
1. It is uncertain whether the int bit segment is treated as a signed or unsigned number.
two。 The maximum number of bits in the segment cannot be determined (16-bit machine maximum 16-bit machine maximum 16-bit machine maximum 32-bit machine maximum).
3. The criteria for allocating memory from left to right or right to left for members in a bit segment has not been defined.
4. When a structure contains two segments, the second segment is too large to accommodate the remaining bits of the first segment, whether it should be discarded or utilized, the standard has not yet been defined.
Action
"what is such a mysterious thing for?" you may have such a question.
Let's take the above scenario to analyze:
Int aura 2 int blux 5 int cburet 10 int dug 30
When we do not allocate a bit segment, we need 16 bytes, but only 8 bytes after allocation. in fact, the bit segment is to save space and act as a flow-saving master.
The prerequisite for the use of bit segments is that some details need to be very clear. If the value of my member a has only four states: 0010, 0110, 11, then is it enough for me to assign 2 bytes to a? then I will give it to 2. If I am an int type and 32 bytes are born, the cost-effective ratio of this saving is not low.
Of course, everything can not be perfect, I will help you save, but there will always be some waste, which is inevitable. To sum up, compared with the structure, the bit segment can achieve the same effect and effectively save space, but there is a cross-platform problem.
Tip
Note that the bit segment mechanism has nothing to do with the stack. It is the bytes of the data that enter the stack, and the bit segments play a role in the internal space of these bytes, which have been detailed to the "bits" and the size end mode mentioned earlier. The size end is the storage mode of consecutive bytes of data, and it can not be refined to bytes.
Application scenario
Bit segments are also widely used in real life, such as the format of packets on our network:
This is how we send information to an object on the Internet. the biggest problem is that if the packet is thrown directly on the Internet, it will be like a tractor on a high speed, and iron traffic jams will cause network congestion. We will use the bit mechanism to reduce the burden on the network.
The above is all the contents of the article "how to realize the bit mechanism in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.