Size End:
For data types such as char in C + +, it is itself a byte size and does not cause problems. However, when the type of the system is int, it needs to occupy 4 bytes (32bit) in the 32bit, and this will result in the order of the 4 bytes stored in the register. such as int maxheight = 0x12345678,&maxheight = 0x0042ffc4. How to store the specific? This is the time to understand the computer's size and end of the principle.
Big Big : (Big-endian) is the value of the high byte placed in the low memory address, the value of the position of bytes in the high address of memory.
Small End: (Little-endian) is the number of high bytes placed in the high address, low byte placed on the low address.
Our common x86 structures are small-end patterns, and most dsp,arm are small-end modes, but some arm can choose the size-end mode. So for the above maxheight should be small-end mode to store, see the following two tables.
| Address |
0x0042ffc4 |
0x0042ffc5 |
0x0042ffc6 |
0x0042ffc7 |
Numerical |
0x78 |
0x56 |
0x34 |
0x12 |
Figure (1) is a small end mode
| Address |
0x0042ffc4 |
0x0042ffc5 |
0x0042ffc6 |
0x0042ffc7 |
| Numerical |
0x12 |
0x34 |
0x56 |
0x78 |
Figure (2) is a big-endian mode
Through the above table, you can see that the size of the end of the difference, where it is not possible to discuss that way better, personally feel that big-ended mode is more in line with my habits . (Note: Here I would like to say, in fact, in the computer memory does not exist in the so-called data types, such as Char,int and so on.) The role of this type in the code is to let the compiler know how many bits of data should be read from that address at a time, assigning it to the corresponding variable. )
Bit fields:
In a computer, binary 0 is used to represent data, each 0 or 1 occupies 1 bits of storage space, 8 bits form a byte (byte), the smallest unit of data type in the computer, such as Char occupies one byte in the 32bit system. However, as we know, sometimes the data in the program may not need such a byte, such as a switch state, only on and off, with 1 and 0, respectively, can be represented. At this point the state of the switch requires only one storage space to meet the requirements. If stored in one byte, it is obvious that another 7-bit storage space is wasted. So in the C language there is a bit (some also called the bit field, is actually a thing) this concept. The specific syntax is after the variable name, plus the colon (:) and the number of bits in the specified storage space. The exact definition syntax is as follows:
Copy Code code as follows:
struct Bit-segment name
{
Bit segment data type bit-segment variable name: bit length;
.......
}
Instance
struct Node
{
Char a:2;
Double i;
int c:4;
}node;
In fact, the definition is very simple, the meaning of the above example is to define a char variable A, occupy 2-bit storage space, a double variable i, and an int variable C that occupies 4 bits of storage. Note that this changes the size of the bytes that the variable used to occupy, not that one of our conventionally defined int variables occupies 4 bytes, and a char variable occupies 11 bytes. Run in the actual running environment, sizeof (node) = 24 is obtained because of byte-aligned memory.