1_ big-endian mode and small-end mode

Source: Internet
Author: User
Tags binary to decimal

Transfer from-http://blog.csdn.net/hackbuteer1/article/details/7722667

In various computer architectures, the storage mechanism of byte, word and so on is different, which leads to a very important problem in the field of communication, that is, the information unit (bits, bytes, words, double words, etc.) communicated by both parties should be transmitted in what order. If no consistent rules are reached, both parties will not be able to properly encode/decode and cause communication failure. There are two main types of byte storage mechanisms commonly used in computers in various systems: Big-endian and Little-endian, which begin with the byte sequence.


first, what is the byte order
BYTE-order, as the name implies the order of bytes, and then say two more than a byte type of data in memory storage order (a byte of data of course there is no need to talk about the order of the problem). In fact, most people in the actual development of the very few direct and byte-order dealings. Byte order is a problem that should be considered only in cross-platform and network programs.

In all the articles that introduce the byte order, the byte order is mentioned in two categories: Big-endian and Little-endian, and the reference standard Big-endian and Little-endian are defined as follows:
A) The Little-endian is the low-bit bytes emitted at the lower address of the memory, high-bit bytes emitted in the memory of the higher address.
b) The Big-endian is the high-bit byte emitted at the low address of the memory, and the low byte is discharged at the upper address of the memory.
c) Network byte order: The TCP/IP layer protocol defines the byte order as Big-endian, so the byte order used in the TCP/IP protocol is often referred to as the network byte order.

1.1 What is the high/low address end
First we need to know the spatial layout of the memory in the C program image: In c expert programming or advanced Programming for UNIX environments, there is a description of the layout of the memory space, roughly as follows:
-----------------------Maximum memory address 0xFFFFFFFF
Bottom of Stack
Stack
Top of Stack
-----------------------

NULL (void)
-----------------------
Heap
-----------------------
Uninitialized data
-----------------------collectively referred to as data segments
Initialized data
-----------------------
Body segment (Code snippet)
-----------------------Minimum memory address 0x00000000
As can be seen from the graph, the stack is growing downward in the memory distribution, and the heap is increasing upward.
For example, if we allocate a unsigned char buf[4] on the stack, how is this array variable laid out on the stack? See:
bottom of stack (high address)
----------
Buf[3]
BUF[2]
BUF[1]
Buf[0]

----------
Top of stack (low address)
In fact, we can create an array ourselves in the compiler, and then output the address of each element of the array separately, to verify.
1.2 What is high/low byte
Now wefigure out the high/low address, then consider the high/low byte。 Some articles say that the lower byte is the least significant bit and the high byte is the most significant bit. If we have a 32-bit unsigned integer 0x12345678, then what is high and what is low? It's actually very simple.In decimal we say that the left side is high, the right side is low, and so is the other system. Take 0x12345678, the bytes from high to low are 0x12, 0x34, 0x56, and 0x78 in turn.
The high/low address end and high/low byte are all clear. Let's review the definitions of Big-endian and Little-endian and illustrate the two byte-sequences with illustrations:
Take unsigned int value = 0x12345678 as an example, and look at its storage in two byte sequences, we can use unsigned char buf[4] to represent value:
Big-endian: Low address storage highSuch as:
Bottom of stack (high address)
---------------
BUF[3] (0x78)--Low
BUF[2] (0x56)
BUF[1] (0x34)
Buf[0] (0x12)--high
---------------
Top of stack (low address)

Little-endian: Low-address storage lowsSuch as:
Bottom of stack (high address)
---------------
BUF[3] (0x12)--high
BUF[2] (0x34)
BUF[1] (0x56)
Buf[0] (0x78)--Low
--------------
Top of stack (low address)

Second, various endian
2.1 Big-endian
A term in computer architecture that describes the order of multi-byte storage in which the most important byte (MSB) is stored at the lowest-end address. The processors with this mechanism are the IBM3700 series, the PDP-10, the Mortolora microprocessor family and the vast majority of RISC processors.
+----------+
| 0x34 |<--0x00000021
+----------+
| 0x12 |<--0x00000020
+----------+
Figure 1: Double-byte number 0x1234 in Big-endian mode with start address 0x00000020

In Big-endian, the sequence number in the bit sequence is as follows (in the case of double-byte 0x8b8a):
Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+-----------------------------------------+
Val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 |
+----------------------------------------+
Figure 2:big-endian Bit Sequence encoding method
2.2 Little-endian
A term in computer architecture that describes the order of multi-byte storage in which the least significant byte (LSB) is stored at the lowest-end address. Processors with this mechanism are PDP-11, VAX,Intel Series microprocessorsAnd some network communication devices. This term is often used to describe the order of emissions for each bit in a byte, in addition to the multi-byte storage sequence.

+----------+
| 0x12 |<--0x00000021
+----------+
| 0x34 |<--0x00000020
+----------+

Figure 3: Double-byte number 0x1234 in Little-endian mode with start address 0x00000020
In Little-endian, the sequence and Big-endian of the bit sequence are exactly the opposite, as follows (in the case of double-byte 0x8b8a):
Bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
+-----------------------------------------+
Val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 |
+-----------------------------------------+
Figure 4:little-endian Bit Sequence encoding method
Note: The host order, which we usually call, follows the Little-endian rule。 Therefore, when the two hosts to communicate through the TCP/IP protocol, it is necessary to call the corresponding function for host order (Little-endian) and network Order (Big-endian) conversion.
CPUs in Little-endian mode are stored in the operands from low to high byte, while the Big-endian mode stores the operands from high to low bytes. The 32bit wide number of 0x12345678 in Little-endian mode CPU memory (assuming that it is stored from the address 0x4000) is as follows:
Memory address 0x4000 0x4001 0x4002 0x4003
Storage content 0x78 0x56 0x34 0x12
In Big-endian mode, the CPU memory is stored in the following way:
Memory address 0x4000 0x4001 0x4002 0x4003
Storage content 0x12 0x34 0x56 0x78
The specific differences are as follows:


Iii. Advantages and disadvantages of Big-endian and Little-endian
Big-endian Pros: By first extracting high-order bytes, you can always determine whether the number is positive or negative by looking at the byte at offset 0. You don't have to know how long this number is, or you don't have to go through some bytes to see if the value contains a sign bit. This value is stored in the order in which they are printed, so functions from binary to decimal are particularly effective. Therefore, for different requirements of the machine, in the design of access mode will be different.

Little-endian Advantages: Extracting one, two, four, or longer byte data assembly instructions in the same way as all other formats: first, the lowest bit byte is fetched at the offset address of 0, because the address offset and the number of bytes are one-to-two relationships, The mathematical function of multiple precision is relatively easy to write.

If you increase the value of the number, you may increase the number on the left (the high-level non-exponential function requires more numbers). Therefore, it is often necessary to increase the number of two digits and move all Big-endian in the memory, moving all numbers to the right, which increases the workload of the computer. However, the non-important bytes in the memory using Little-endian can exist in its original position, and the new number can exist in its right high address. This means that some computations in the computer can become simpler and faster.
Please write a C function, if the processor is Big_endian, then return 0, if Little_endian, then return 1.

[CPP]View PlainCopy
    1. int checkcpu (void)
    2. {
    3. Union
    4. {
    5. int A;
    6. Char b;
    7. }c;
    8. C.A = 1;
    9. return (c.b = = 1);
    10. }

Anatomy: Because the Union union is stored in the order that all members are stored from the low address, this feature makes it easy to obtain CPU-to-memory Little-endian or Big-endian mode reads and writes.
Description
1 in C, the data members of a consortium (a common body) are stored from the low address.
2 If the small-end mode, from the low address to the high address c.a stored as 0x01 xx 00,c.b is assigned to 0x01;
————————————————————————————
Address 0x00000000 0x00000001 0x00000002 0x00000003
C.A 01 00 00 00
c.b 01 00
————————————————————————————
3 If the big-endian mode, from the low address to high address C.A storage for 0x00 xx 01,c.b is assigned to 0x0;
————————————————————————————
Address 0x00000000 0x00000001 0x00000002 0x00000003
C.A 00 00 00 01
c.b 00 00
————————————————————————————
4 depending on the value of the C.B, you can determine the mode of the CPU.

For example, a 16 binary number is 0x11 22 33, where it is stored
Store 11 in Address 0x3000
Store 22 in Address 0x3001
Store 33 in Address 0x3002
It's written in the address 0x3000-0x3002. Data is stored in the 0x112233
And this kind of storage and representation, just in line with the big-endian.
Another good understanding of the wording is as follows:

[CPP]View PlainCopy
  1. BOOL Checkcpu () //If it is in big-endian mode, return True
  2. {
  3. short int test = 0x1234;
  4. if (* (char *) &test) = = 0x12) //Low address holds high-byte data
  5. return true;
  6. Else
  7. return false;
  8. }
  9. int main (void)
  10. {
  11. if (!CHECKCPU ())
  12. cout<<"Little endian" <<endl;
  13. Else
  14. cout<<"Big endian" <<endl;
  15. return 0;
  16. }

Or the following two kinds of writing are also possible

[CPP]View PlainCopy
  1. int main (void)
  2. {
  3. short int a = 0x1234;
  4. char *p = (char *) &a;
  5. if (*p = = 0x34)
  6. cout<<"Little endian" <<endl;
  7. Else
  8. cout<<"Big endian" <<endl;
  9. return 0;
  10. }
  11. int main (void)
  12. {
  13. short int a = 0x1234;
  14. char x0, X1;
  15. x0 = ((char *) &a) [0];
  16. X1 = ((char *) &a) [1];
  17. if (x0 = = 0x34)
  18. cout<<"Little endian" <<endl;
  19. Else
  20. cout<<"Big endian" <<endl;
  21. return 0;
  22. }

1_ big-endian mode and small-end mode

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.