"Turn" easy to remember the meaning of the small end of big-endian (with the explanation of big and small end)

Source: Internet
Author: User

Original URL: http://www.cnblogs.com/wuyuegb2312/archive/2013/06/08/3126510.html

Perhaps you have a careful understanding of what is the big end of the small side, but also to write a test on the machine on hand is big or small end of the program, and even write a big-endian small-end conversion program; But after a while, when you see the two words of big and small end, your brain quickly floats the work that you have done, But always can't remember which kind is big-endian, which kind is the small end, then goes to check the record that writes before? What's even more unpleasant is that this kind of experience is so annoying. If you have had this unpleasant experience like the previous author, then this article hopes to help you to completely solve this distress and let you thoroughly remember them.

This article is not for you if you often use big and small ends in your work so that you are familiar with them, or if your memory is excellent in terms of length and accuracy, so that you do not need other methods.

If you do not know what is big and small end before reading this article, then you can refer to the appendix of this article or other blog post, the relevant introduction is very much, and the appendix provides a very common explanation and a test program, and then look at the text.

In order to help remember, understanding is necessary, and the purpose of memory, that is, why remember it, is more important. Perhaps you will ask, first understand the concept, use the time to check again, no? Actually, I thought so before. Big and small end of these two nouns, you will be in a lot about network programming, system design, and even code writing books to see, and it is a lot of company's written questions, interview questions popular content, it is very common in some areas. If you wait until you use the time to check, on the one hand to reduce your efficiency, on the other hand, when the test is not you want to check can be checked; in fact, the main thing is, after mastering the rules, it is not difficult to remember them.

  Let's start by understanding how the two confusing terms, big-endian and small-ended, are produced. the 9th chapter of programming practice mentions that "big-endian" and "small-end" can be traced back to Jonathan Swift's "Gulliver's Travels" of 1726, and one of the two countries is arguing over whether the eggs are first broken by the larger end or the smaller one. Even the war broke out. 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. 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. People with a little bit of common sense will know that it is hard to guess the right meaning if you understand slang by literal means. 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.

But to my inspiration, in the Qiu Zongyan translation of "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 the end of the high address or low address, its in-memory method is very intuitive, such as:

"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.

Appendix: What are "big-endian" and "small" and a piece of code that tests the big or small side of the machine?

  (This text is a generalization of the concept of "UNIX network programming, Volume One," which is not limited to this book, but many computer books introduce this concept, and you will encounter them in different fields of computer-related books.) While it's confusing, it's best to understand the concepts of these two words before reading the text. Of course, if you've ever touched them as you've described in the text, you don't have to read this part. After reading you will find that although you understand the meaning, but it is easy to forget, then you can see the text part of it)

For a 16-bit integer consisting of 2 bytes, there are two ways to store these two bytes in memory: One is to store the low-order bytes at the start address, which is called the small-end (Little-endian) byte order, and the other is to store the high-order bytes at the start address, which is called the big-endian (Big-endian ) byte order.

  

In the figure, the top indicates that the memory address grows in the direction from right to left, and at the bottom indicates that the memory address grows in the direction left to right. It also indicates that the most significant bit (most significant BIT,MSB) is the leftmost bit of this 16-bit value, and the least significant bit (least significant bit, LSB) is the rightmost bit of this 16-bit value. The term "small end" and "big endian" indicate which end (small or big) of multiple byte values is stored at the starting address of the value.

Both of these byte sequences are not standard and are used by the system. The byte order used by a given system is called the host byte order, and the host byte order can be output with the following program. The method is to determine the byte order by holding a 2-byte value 0x0102 in a short integer variable, and then viewing its contiguous byte c[0] (corresponding to address a) and c[1] (corresponding to address a+1).

#include <stdlib.h> #include <stdio.h>int main (int argc, char **argv) {    Union {short        s;        Char c[sizeof (short)];    } UN;    UN.S = 0x0102;    if (sizeof (short) ==2) {        if (un.c[0]==1 && un.c[1] = = 2)            printf ("big-endian\n");        else if (un.c[0] = = 2 && un.c[1] = = 1)            printf ("little-endian\n");        else            printf ("unknown\n");    } else        printf ("sizeof (short) =%d\n", sizeof (short));    Exit (0);}

  

"Turn" easy to remember the meaning of the small end of big-endian (with the explanation of big and small end)

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.