How variables are stored in memory-----"big-endian" and "small"

Source: Internet
Author: User

"Big-endian" and "small end" can be traced back to Jonathan Swift's "Gulliver's Travels" in 1726, and one of the two countries was arguing over whether to break the larger end first or break the smaller end first, or even burst into war. In October 1981, Danny Cohen's article "on jihad and Prayers for Peace" (on Holy Wars and a plea for peace) introduced this pair of words into the Computer World (chapter 9th of the programming practice). So, the so-called big and small end, that is, Big-endian and Little-endian, in fact, from the description of the area of the egg and extended to the description of the computer address, it can be said, is derived from a slang term computer terminology. In the computer, the description of the address, rarely used "big" and "small" to describe, corresponding to the use of more "high" and "low"; Unfortunately, the term literally translated directly into the "large" and "small End", it is not very strange to confuse people.

If a 32-bit integer 0x12345678 is stored in an integer variable (int), this integer variable is stored in memory by the big-endian or small-end pattern, as shown in the following table. For simplicity, OP0 is used here to represent the highest byte MSB (most significant byte) of a 32-bit data, using OP3 to represent the lowest byte lsb (leastsignificant byte) of a 32-bit data.

Address offset

Big-endian mode

Small terminal mode

0x00

(OP0)

(OP3)

0x01

(OP1)

(OP2)

0x02

(OP2)

(OP1)

0x03

(OP3)

(OP0)

If a 16-bit integer 0x1234 is stored in a short integer variable (shorter). This short integer variable is stored in memory in the size end pattern as shown in the following table.

address offset

big-endian mode

0x00

12 (OP0)

34 (OP1)

0x01

34 (OP1)

12 (OP0)

As is known from the above table, the main difference between the data storage in the size mode is the byte order in which the big end is stored in the low address, and the small-ended method stores the high-level address. The use of big-endian data storage in line with human normal thinking, and the use of small-end method of data storage is conducive to computer processing. So far, the use of big-endian or small-end data storage, the merits of the pros and cons are inconclusive.

Some processor systems use a small-end approach for data storage, such as Intel's Pentium. Some processor systems use a big-endian approach for data storage, such as IBM Semiconductors and Freescale PowerPC processors. Not only for the processor, some peripheral design also has the use of big-endian or small-end data storage options.

Therefore, in a processor system, there may be a big-endian and small-end mode simultaneously exist phenomenon. This phenomenon for the system hardware and software design brings a big trouble, which requires the system design engineer, must deeply understand the big-endian and small-end mode differences. The difference between the big end and the small terminal mode is embodied in a processor register, instruction set, system bus and other levels.

       in Qiu Zongyan translation of the "Programming practice", the terminology is not translated as "big end" and small end, but "high tail" and "low End", it is good to understand: if a number as a string, such as 11223344 as "11223344", the end is a ' "," 11 ' to ' 44 ' occupies a storage unit, then its tail is obviously 44, the front of the high or must not indicates that the tail is placed at a high address or low address, its in-memory method is very intuitive,

"High/Low end" is more difficult to confuse than "big/small End". However, according to personal experience, in the market books, the network of all kinds of information, unfortunately, the former has been very rarely seen, more than the latter. Fortunately, in these two pairs of adjectives, just "high" and "big" correspondence, "low" and "small" correspondence; Since the high end is the big end, the lower end corresponds to the small end, then when you see the big and small end of the term, you can turn them into high tail and low end in the brain, then with the previous understanding, even without memory, Thinking about the literal meaning of high and low can recall their meaning. But it is strange that the same is the Qiu Zongyan translation of "Programming Original" (Elements of programming), but the Big-endian translated into the Big Tail format (Chapter one).

After understanding, to summarize, the method of memory is:

(data as a string) big end --high tail, small end--low end   a little bit of thinking about what is "high", What is "low", "tail end" is what, the problem solved, and then do not worry about being "big" and "small End" confused. In this way, it is time to give up the original rote memorization and easy to wrap themselves in the confusion of understanding.

How to tell if the CPU in the system is little endian or big endian mode?
Analysis:
As a computer-related professional person, we should be in the computer composition have learned what is called little endian and big endian. Little endian and big endian are two different sequences of CPU storage data. For data types such as Integer, Long Integer, Big endian thinks the first byte is the highest byte (the high byte to the low byte of the data from the low address to the high address), and the little endian the opposite, It considers the first byte to be the lowest byte (the low byte to the high byte) that holds the data in the order from the lower address to the higher address.
For example, suppose you start with the following data from the memory address 0x0000:
0x12 0x34 0xAB 0xCD
If we go to read a four-byte variable with an address of 0x0000, if the byte order is Big-endian, the result is read 0X1234ABCD, and if the byte-order bit Little-endian, the result is read 0xcdab3412. If we write 0X1234ABCD to memory starting with 0x0000, the results of little endian and big endian mode are as follows:
Address 0x0000 0x0001 0x0002 0x0003
Big-endian 0x12 0x34 0xAB 0xCD
Little-endian 0xCD 0xab0x34 0x12
In general, x86 series CPUs are Little-endian byte-order, PowerPC is usually big endian, and some CPUs can set the CPU to work in little endian or big endian mode via jumpers.
Answer:
Obviously, the solution to this problem is only to store data of one byte (Char/byte type) and one integer data in the same memory
Start address, by reading the integer data, analyze the Char/byte data in the integer data high or low to determine the CPU working in little
Endian is still the big endian mode. Come to the following answer:

typedef unsigned char BYTE;

int main (int argc, char* argv[])

{

unsigned int num,*p;

p = #

num = 0;

* (BYTE *) p = 0xFF;

if (num = = 0xff)

{

printf ("little\n");

}

else//num = = 0xff000000

{

printf ("big\n");

}

return 0;

}

Is there a better way than the above method (cast by pointer type and assign values to the first byte of the integer data to determine whether the assignment is assigned to a high or low)? We know that the members of the union themselves are stored in the same memory space (shared memory, where the Union plays a role and contribute), so we can combine a char/byte data and an integer data as a union member to get the following answer:

int Checkcpu ()

{

{

Union W

{

int A;

Char b;

C

C.a=1;

return (c.b==1);

}

}

To achieve the same functionality, let's look at how the relevant source code in the Linux operating system is done:
Static Union {char c[4]; unsigned long mylong;} endian_test = {{' L ', '? ', '? ', ' B '}};
#define ENDIANNESS ((char) endian_test.mylong)
Linux Kernel authors use only a union variable and a simple macro definition to implement the same functionality as a large piece of code! From the above piece of code we can deeply understand the subtlety of the Linux source code! (If endianness= ' l ' indicates that the system is little endian,
For ' B ' means big endian)

Reference article 1

Reference Article 2



How variables are stored in memory-----"big-endian" and "small"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.