Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the bit domain of the C language structure?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the location domain of the C language structure". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the location domain of the C language structure?"

When we talk about structures before, we use data types such as int,char to define the member variables of structures. one thing these member variables have in common is that they are all one byte or even times the length of one. However, when we store some information, we don't need a complete one, but we just need to make the variable occupy one or more binary bits.

Some readers may think, then I can directly use a byte length to store these several bits of variables, although it will waste some storage space, but the waste of these bits is irrelevant to some computers or single-chip computers nowadays. Indeed, when we define a variable, for the current powerful computer hardware, defining a variable with a few bits and defining a variable with a few bytes does not have any effect. On the contrary, the byte unit variable has more storage space than the bit variable, which can effectively prevent length overflow.

However, when faced with the following applications, byte unit variables not only do not have any benefits, but will greatly increase the difficulty of our program operation. For example, there is a register in a single-chip microcomputer system. Assuming that the length of this register is one byte, its function is to control the timer of a single-chip microcomputer and to reflect the state of a timer. The 7th and 6th bits of this register represent the status bits of the timer TIM_STAT [1:0], the 5th, 4th and 3rd bits represent the frequency division coefficient TIM_DIV [3:0] of the timer clock source, and the first bit represents the overflow flag TIM_OVERFLOW of the timer. Bit 0 represents the working switch TIM_START/STOP of the timer. The registers for this assumption are shown in figure 1.

Fig. 1 a register

In the face of the above application, our general practice is to define a variable Tim_Ctrl of type unsigned char, and then perform bit operations. For example, to assign TIM_STAT to the state 0b11, we can use the bit operation statement, "Tim_Ctrl | = 0b11000000;". If you want to assign TIM_STST to the state 0b00, use the bit operation statement "Tim_Ctrl & = ~ (b11000000)" This kind of micromanipulation is very tedious and unintuitive.

Is there a data type that can support a variable with a small number of digits? For example, you can directly define a two-digit variable, and then assign a state 0b11. In the C language, conventional variables obviously do not support this operation, but they do so in structures. This way of supporting bit operations in the C language structure is called "bit field", or "bit segment".

Bit segment (or "bit field", Bit field) is a data structure that stores data in a compact form of bits and allows programmers to manipulate the bits of this structure. The benefits of this data structure are:

Data units can save storage space, which is especially important when a program needs thousands of data units.

Bit segments can easily access parts of an integer value, thus simplifying the program source code.

The disadvantage of bit field data structure is that the implementation of memory allocation and memory alignment depends on specific machines and systems, and there may be different results in different platforms, which leads to the fact that bit segments are essentially unportable.

The bit field is defined when it is defined in the structure, as follows:

Struct

{

Data type variable name: bit length

Data type variable name: bit length

} status

For example, for the register of the timer above, we can define it as follows:

Struct

{

Unsigned char TIM_START_STOP: 1

Unsigned char TIM_OVERFLOW: 1

Unsigned char TIM_DIV: 4

Unsigned char TIM_STAT: 2

} register

Note that after this definition, the bit field definition of the entire structure starts with a low address. Therefore, when a bit field is defined, its memory distribution is shown in figure 2.

Figure 2 Bitfield memory allocation

Once the bit field is defined, such as TIM_DIV in figure 2, the number of bits it occupies is 4bits, so although we define it with the unsigned char type, the maximum binary number it can represent is only 4 digits, that is, the range is: 0x00~0x0F. Once our assignment exceeds the secondary range, this variable discards the excess high-order data.

We can write a program to demonstrate that, according to the register bit distribution shown in figure 1, define the structure bit field variable, then assign it a value beyond its length, and then print it out to see the output. As shown in figure 3.

Fig. 3 structure bit domain members out of range

As we can see from figure 3, once a bit field member exceeds its bit size, the compiler first throws a warning, and then prints out the wrong value for this variable. So why do we assign a value of 20 and output a 4? This is because the binary number of 20 is 0b00010100, and because the TIM_DIV variable occupies only 4 bit storage space, the excess is discarded, leaving only the lower 4-bit 0b0100, so this variable prints out a value of 4.

Because this structure variable takes up one byte of storage space, we can print out the entire contents of this storage space with a pointer. The operation is also very simple, we just need to define a pointer of type unsignedchar, and forcibly convert the address of the structure to a "unsignedchar *" type, then point to it with a pointer, and finally print out the address with a reference pointer, and you can see the whole picture of the structure. The specific operation is shown in figure 4.

Fig. 4 structural data

Why did it end up with 0xA7? Because the whole structure is assigned according to the bit field, as shown in figure 5, and finally converted to hexadecimal is 0xA7.

Figure 5 after bitfield assignment

At this point, I believe you have a deeper understanding of "what is the bit domain of the C language structure". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report