Big-Endian and Little-Endian | one minute series

Tags: Public No. Articles


Users angle memory

For those of us farm yard, the memory is equivalent to the number of consecutive lattice composition, draw a map that is this:

image_1di3049vv1lltbflfm71dkkgqb9.png-14.1kB

Total memory structure as shown by a ncontinuous grid, each grid may store a byte of data (8 bits = 1 byte). Each grid has its unique number, called 内存地址. Then:

  • If our computers are 32-bit words, the number of lattice theory of memory that can be accessed is 2³² (ie 4GB), the corresponding memory address range is: 0 ~ 2³² - 1.

  • If our computers are 64-bit words of memory can be accessed by the number of lattice theory is 2⁶⁴ (this is a very big number), the corresponding memory address range is: 0 ~ 2⁶⁴ - 1.

Right bytes weight

We know that computers use to represent binary data, but for humans too unfriendly binary, convert them more comfortable to look at some hex, say this for the following four bytes of binary data (word There is no space between the festival, just as everyone looks beautiful and I was by a space):

00010010 00110100 01010110 01111000
复制代码

Binary byte 00010010may be converted to hexadecimal 0x12, binary byte 00110100may be converted to hexadecimal 0x34, binary byte 01010110may be converted to hexadecimal 0x56, binary byte 01111000may be converted to hexadecimal 0x78. So the whole numbers into hexadecimal data is:

0x12345678
复制代码

among them:

  • Byte 0x12on the left, representing 1 × 16⁷ + 2 × 16⁶this 0x12byte account the weight is relatively large.

  • Byte 0x34represents 3 × 16⁵ + 4 × 16⁴, this byte accounting for the weight of the second.

  • Followed by a byte 0x56, representing 5 × 16³ + 6 × 16², accounting for the weight of this byte again.

  • Finally, bytes 0x78, representing 7 × 16¹ + 8 × 16⁰this 0x78byte account the weight is minimal.

Type of high-level language

We generally use high-level language programming (as distinguished from machine language or assembly language), there is a high level language 类型concepts, some types of data to be occupied by several consecutive memory grid. Let's say there is a kind of a language called inttype, that type take 4 bytes in size. If we want high-level language in this number (hexadecimal) below the set inttype:

0x12345678
复制代码

Then this data stuffed in what way the memory lattice it? This is the high weight into the low byte address, or the high weight into the high byte address it? This fact, and we should start eating egg bulk of skin peeling eggs or egg peeling skin head start small problem a little better about the same, in fact, nothing different. But start with the design of the computer in advance because the deal is good uncle who caused such consequences:

  • Some people have high weight placed low byte address, people multi-byte data represented by such way is called 大端法:

    image_1di34u5pk1nv5k1n1fpqorbbkmm.png-24.6kB

  • Some people have high weight placed high byte address, people multi-byte data represented by such way is called 小端法:

    image_1di34v19poj12cl1tomu4395i13.png-24.6kB

Since it did not initially a good discussion, leading to multi-byte data using different representations on different machines, which for portability program had an impact. In addition, the network has become an integral part of modern society, when the machine will send a multi-byte data representation from one use to a big-endian method used when the machine represented the small end process, will result in errors, so uncle provision of network design, data over the network during transmission, must be unified using big endian method for transmission, then use the machine for the small end of the law, it is much more cost crop formalities and then transferred chant ~

Digression

Writing articles tiring, sometimes you feel very smooth read, that is actually behind the result of numerous modifications. If you feel good please help forward it extremely grateful ~ Here is my public number "We are small frog" technology inside more dry goods, from time to time about pulling a calf, welcome attention:

Guess you like

Origin juejin.im/post/5d633a15f265da03c5031b5c