In the data structure, questions about the data storage method of "large and small end"

Source: Internet
Author: User
Endian, big-Endian, and little-Endian)

Reprinted from: http://www.cppblog.com/tx7do/archive/2009/01/06/71276.html

In various computer architectures, the storage mechanisms for bytes and words are different, which leads to a very important issue in the computer communication field, that is, the order in which information units (bit, byte, word, double-word, etc.) of communication between the communication parties should be transmitted. If no agreement is reached, the communication fails due to incorrect encoding/decoding. At present, there are two types of byte storage mechanisms commonly used in computer systems:

Big-edian and little-Endian.

 

 

Byte sequence endian

Modern computer systems generally useBytes(Octet, 8-bit byte) as the unit of logical addressing. When the length of a physical unit is greater than 1 byte, it must be distinguishedByte order(Byte order, or
Endianness). There are two common byte sequences:Big endian(High-byte first) andLittle endian(Low-byte first ).BeAndLe. Intel x86 Platform uses little
While the PowerPC processor uses the big endian. For example, integer number $1234 ABCDThere are two storage methods:

Byte order

Memory Data

Remarks

Big endian (be)

0xab 0xcd 0x12 0x34

At this time, 0xab is calledMost significant byte(MSB)

Little endian (LE)

0xcd 0xab 0x34 0x12

At this time, 0xcd is calledLeast significant byte(LSB)

 

Source: According to jargon file, the term endian comes from Jonathan Swift's ironic novel "Gulliver's Travels" (garifo Travel Notes) in 1726. This novel encountered the following scenario when describing Gulliver's Changyou minor country. The villain in a country is very small (6 inch in height) and always encounters unexpected problems. Once, the argument that boiled eggs should be peeled off from the big end or the little end caused a war, and formed two opposing teams: Swift, the one who supports big-end exploitation, is called Big-endians, and the one who supports little-end exploitation is called Little-endians ...... (The suffix Ian indicates the person who supports a certain opinion :-). The term endian comes from this.

1980, danny Cohen cited this article in his famous paper "on holy wars and a plea for peace" to calm down a debate on the order in which bytes should be transmitted in the message word. In this article, Cohen uses the big-endians group that supports message sequence transmission starting from MSB, the relative transfer from LSB is called Little-endians. Since then, the term endian has been widely used in this paper.

Mapping registers to memory locations

 

 

Maximum valid bits MSB: Most Significant Bit

The highest valid bit (MSB), sometimes called the leftmost bit, is the n-1 bit in an N-bit binary number, which has the highest weight (2 ^ (n-1 )). The first or leftmost digit when the number is written in a normal way.

 

 

Minimum valid LSB: least significant bit

The least valid bits (LSB) is a binary integer location for these unit values, which determines whether the number is an even or odd number. LSB sometimes refers to the rightmost bit, because the Protocol writes the less important number to the right position symbol. It is similar to the least important digit of a decimal integer. It is a number at the (rightmost) position.

 

 

Big-Endian

The maximum valid bits (MSB) are stored at the low address of the memory, and the low byte is discharged at the high address of the memory.

A term in the computer architecture that describes the order of Multi-byte storage. In this mechanism, the maximum valid bit (MSB) is stored on the lowest end address. The ibm3700 series, PDP-10, mortolora microprocessor series and the vast majority of them are processors using this mechanism.

 

 

Little-Endian

The low address stores the lowest valid bits (LSB), which is the low address of the memory and the high address of the memory.

A term in the computer architecture that describes the order of Multi-byte storage. In this mechanism, the least important bytes (LSB) are stored on the lowest-end address. Processors using this mechanism include PDP-11, VAX, Intel series microprocessor, and some network communication devices. In addition to the multi-byte storage sequence, this term is often used to describe the emission sequence of each bit in a single byte.

 

 

Mid-range middle-Endian

In addition to big-Endian and little-Endian, the multi-byte storage sequence is middle-Endian. For example, four bytes are used as an example: middle-Endian is stored in the order of 3-4-1-2 or 2-1-4-3. This storage sequence occasionally appears in the compression format of decimal numbers in some minicomputers.

 

 

Network byte order

TCP/IP Protocols define the byte sequence as big-Endian. Therefore, the byte sequence used in TCP/IP is usually called the network byte sequence.

 

 

Host orader

It follows the little-Endian rule. Therefore, when the two hosts need to communicate through the TCP/IP protocol, the corresponding function needs to be called for the host Order (little-Endian) and network Order (big-Endian).

 

 

How does C ++ determine the big end and small end?

Macro method:

Const   Int Endian =   1 ;

# Define Is_bigendian () (* (char *) & endian) = 0)

# Define Is_littlendbian () (* (char *) & endian) = 1)

Method 2:

Bool Islittleendian ()

{

Union

{

LongVal;

CharChar [Sizeof(Long)];

} U;

// 1-Intel; 0-Motor)

U. Val =   1 ;

If (U. Char [ 0 ] =   1 )

{

//Small Terminal

Return True;

}

Else   If (U. Char [ Sizeof ( Long ) - 1 ] =   1 )

{

//Big End

Return False;

}  

Throw ( " Unknown! " );

}

 

 

Knowledge

Java uses big-Endian.

 

 

Reference

1. About endian size-end mode:
Http://hi.baidu.com/%C8%FD%C9%EE/blog/item/6abb3d7779c0961db151b96b.html

2. endianness:
Http://en.wikipedia.org/wiki/Endianness

Posted on Yang Xiaobo (5523)
Comment (1) EDIT favorites
Reference

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.