How to determine the current storage environment is big-endian or little-endian mode?

First, the image memory grid.
1, we all know that the data is stored in memory, but it is exactly what is stored one do? Here we introduce a "memory check" to visualize the stored data. In a 32-bit computer, assuming a grid representative of a byte, four-byte cell is such that:

0000 0000 0000 0000 0000 0000 0000 0000

Just a good 8-bit byte, four bytes four grid is 32, and this is why, in a 32-bit machine an int is four bytes, and int is an integer, based on the current number of bits the machine is, if the 64-bit int type is 8 bytes.

Second, the memory lattice address.
1, it has been said above is a byte to represent a grid, with each grid there are 8-bit, but when we want to read a data grid is how to read it? The answer is addressed by address, CPU will be assigned a memory address for each grid, the address is from left to right from low to high. If a store a byte data type, we need only address the grid; if a variable is 4 bytes of data need it? It occupies four lattice address in the end is which address access? The answer is by four grid of the address of the head of the grid, also known as the first address. Therefore, 32-bit machine, defined int a [10], will find a [0] address and the address of a [1] is not a difference, but the difference of 4, since a [0] accounted for 4 bytes, whichever is the first address, a [1] also accounted for four bytes, whichever is the first address, just a difference of 4 is good.

Third, the size of the terminal mode.
1, the small end mode: When the high address in memory lattice grid of a binary number in the upper or lower memory addresses in the lower lattice, it is called the Little endian.

2, big-endian mode: the opposite ends of a small mode, data is placed on the upper grid of low memory address, the data is placed lower or a lattice of high memory addresses.

3, how to understand it? For example: 32-bit machine, if a binary 1, that is 1 int a = 0,000,000,000,000,001 (which we write out), but it need not be like this in the computer storage time, there are two possibilities ,as follows:

0000 0000 0000 0000 0000 0000 0000 0001
1000 0000 0000 0000 0000 0000 0000 0000

So, we see this in two ways, the first way is to store binary 1 is low on the first grid (that is, low grid address), so the first is stored in little-endian mode. Look at the second, a binary 1 is low in the last grid (ie lattice high addresses), the second is the memory of the big-endian mode.

Fourth, how to determine the current storage environment is big-endian or little-endian mode?

1, using a common method for judging the body.

#include <stdio.h>
union test
{
    int a; 
    char b;
};
int main(void)
{
    union test t1;
    t1.a = 1;
    if(t1.b == 1)
    {
        printf("小端模式\n");
    }
    else
    {
        printf("大端模式\n");
    }
    return 0;
}

Thinking about the code is tested: the definition of a common body, as is the union of all the variables which share the same memory, when the int a = 1, the storage 1 may be based on the two apparent:

0000 0000 0000 0000 0000 0000 0000 0001
1000 0000 0000 0000 0000 0000 0000 0000

Then, char type b is shared with a memory, and b is the length of a byte, it must be the first lattice b inside the content, it is determined in the end b is 0 or 1 equivalent of a lattice is determined is 0 or 1, can obtain the least significant bit of the least significant bit is stored in an address grid lattice or most significant bit of the address, it can be judged big endian or little endian mode.

2, is determined using the method pointers.

#include <stdio.h>
int main(void)
{
    int a = 1;  
    char *pa = (char*)(&a);
    if((*pa) == 1)
    {
        printf("小端模式\n");
    }
    else
    {
        printf("大端模式\n");
    }
    return 0;
}

Code inspection is probably thinking: Like the above principles, first defined int a = 1; then type char pointer to access this memory, pointers to char is certainly used to read the first grid, If an explanation is on the low low address, it is little-endian mode, otherwise it is big-endian mode.

3, the size of some seemingly test mode terminal but not the actual manner:
(1) the position, or the position

    int a = 1;
    int b;
    b = a & 0x01;       // 判断是1还是0来判断

Reason: when a & 0x01, a memory has been read out from the & rather than directly on the level of memory, it is not desirable.

(2) displacement

    int a = 1;
    int b;
    b = a>>1;           // 通过右移得出是1还是0判断

The reason: When performing a >> 1 Shi, a read from memory is already out instead >> directly on the memory level, so it is not desirable.

(3) cast

    int a = 1;
    char b;
    b = (char)a;        // 通过强制类型转换得出是1还是0判断

The reason: As above, during the time of a cast, a read from memory what number, and then the cast, which is meaningless.

V. Summary.
1, we found that not all methods can verify the size of the terminal mode, if the operation level C language belongs to the level of operations, we need to start with this number to memory read operations, not directly in the memory hierarchy, such methods are not available.

2, like with the pointer, as a union of this data is read out from the memory the moment it can know what mode, and then read out instead operation.

Published 24 original articles · won praise 27 · views 10000 +

Guess you like

Origin blog.csdn.net/gyyu32g/article/details/79097169