Most significant bit, last (least) Significant Bit
Maximum valid bits (MSB)
The highest value in binary. In a 16-bit digital audio, its 1st BITs have the greatest impact on the 16-bit numeric value. For example
Of the decimal number 15,389, the number equivalent to one row (1) of tens of thousands has the largest impact on the value. Compare the "least valid bits" (LSB ).
Before MSB's high position
Low-level leading LSB.
Speaking of the byte sequence, there must be two major CPU factions. That is, Motorola's PowerPC series CPU and Intel's X86 series CPU. Powerpc
The series uses big endian to store data, while the x86 series uses little endian to store data. So what is big endian?
What about little endian?
In fact, big endian refers to the storage of the highest valid byte (MSB) at a low address, while
Little endian stores the lowest valid byte (LSB) at a low address ).
Text descriptions may be abstract. The following uses images to describe them. Such as numbers
The storage order of 0x12345678 in two different bytecode CPUs is as follows:
Big endian
Low Land
High address
----------------------------------------->
+-+
| 12 | 34 | 56 | 78 |
+-+
Little endian
Low
High address
----------------------------------------->
+-+
| 78 | 56 | 34 | 12 |
+-+
Slave
The two figures above show that using big endian to store data is in line with our human habits of thinking. And little endian ,! @ # $ % ^ & *, Go to hell
-_-|
Why should we pay attention to the issue of byte order? You may ask this question. Of course, if the program you write runs only in a single-host environment and does not work with other programs
Therefore, you can ignore the existence of byte order. But what if your program needs to interact with other programs? Here I want to talk about two languages. C/C ++
The sequence is related to the CPU where the compilation platform is located, while the program compiled by J *** A only uses the big endian method to store data. Imagine if you use the C/C ++ language on the x86 Platform
What is the result of Interworking with other J *** A programs? Take the above 0x12345678 as an example, the data your program transmits to others will point
The pointer of 0x12345678 is passed to J *** a program. Because J *** A uses the big endian method to store data, it naturally translates your data
0x78563412. What? Is it actually another number? Yes, that's the consequence. Therefore, it is necessary to convert the byte order before your C program passes to J ***.
None
Coincidentally, all network protocols use big endian to transmit data. So sometimes we call the big endian method the byte order of the network. When the two servers are different
For communication between a host in the byte sequence, data must be converted to the network in the byte sequence before being transmitted. Ansi c provides the following four macros for converting the byte order.
· Be and
Le
I talked about the byte sequence in the big endian and little endian articles in August 9. For the original article, see the hyperlink above. Yes
Some may still ask if the order between the eight bits in one byte is also divided into big endian and little endian when the CPU stores data in one byte? Or whether
Is there a difference in bits?
In fact, this bit sequence also exists. The following figure shows the number 0xb4 (10110100.
Big endian
MSB LSB
---------------------------------------------->
+- +
| 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
+- +
Little endian
LSB MSB
---------------------------------------------->
+- +
| 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 |
+- +
Real
Because the minimum unit of CPU data storage operations is one byte, the bit order inside the CPU is a black box for our program. That is to say, you give me a finger pointing to the number 0xb4.
For a CPU in the big endian mode, it reads 8 bits from left to right. For a CPU in the little endian mode
It reads 8 bits of this number from right to left. Our program obtains 0xb4 after accessing through this pointer. The bit order inside the byte is invisible to the program. In fact, this is true
The same is true for single-host bytes.
Someone may ask, what if it is network transmission? Will there be problems? Is it necessary to convert the bit through any function?
Order? Well, this is a good question. Assume that the CPU in little endian mode is transferred to the big endian mode CPU in one byte, it will be locally before transmission.
Read the 8-bit number and then transmit the 8 bits in the byte order of the network. In this way, no problem will occur at the receiving end. If we want to transmit a 32-bit number
Littel endian occupies 4 bytes of storage, while network transmission is in bytes. The CPU of little endian reads the first byte and then sends the message.
The Byte above is the original number of LSB. When it reaches the receiver, it becomes MSB, causing confusion.