Web Development Basics-byte order

Source: Internet
Author: User

1. Valid bits

Before we talk about the byte sequence, we need to understand the effective bit, the effective bits are divided into two kinds: the least significant bit (lsb:least significant bit) and the most significant bit (msb:most significant bit). In binary numbers, the LSB is the lowest plus, similar to the rightmost one in the decimal number; The MSB is the highest plus, similar to the leftmost one in the decimal number. In general, the MSB is on the leftmost side of the binary number, and the LSB is at the far right of the binary number. Take a decimal number 12345678 For example, the most significant bit is 1, the least significant bit is 8.

2. Byte order:

The order of bytes, as the name implies, is the order of the data in memory that is larger than the byte type (one byte of the data is of course no need to talk about order). The byte order is divided into two categories: Big-endian and Little-endian, which are defined in the following:

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.

The text description may be more abstract, the following is illustrated with an image. For example, the order in which the digital 0x12345678 is stored in two different byte-order CPUs is as follows:

As can be seen from the above two figure, the use of big endian way to store data is in line with our human thinking habits.

Why pay attention to the problem of byte order? You might ask. Of course, if you write a program that only runs under a stand-alone environment and doesn't deal with other people's programs, you can completely ignore the existence of the byte-order. But what if your program has to interact with other people's programs? Here I would like to speak two languages. In a program written in the C + + language, the data is stored in a sequence that is related to the CPU on which the build platform resides, while Java programs only use the big endian to store data. Imagine what would happen if you wrote a program written under the x86 platform with a C + + language that would interoperate with someone else's Java program? Take the above 0x12345678, your program passed to someone else's data, will point to the 0x12345678 pointer to the Java program, because Java takes the big endian way to store data, it is natural that it will translate your data into 0x78563412. What the? Turned into another number? Yes, that's the result. Therefore, it is necessary to convert the byte order before your C program is passed to the Java program.

advantages and disadvantages of 3.big-endian and Little-endian

Big-endian Advantages: It is easy to distinguish the positive and negative of a number, as long as a byte in the Offset0 place can be confirmed. 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.

Little-endian Advantages: The length is 1,2,4 byte number, the arrangement is the same, the data type conversion is very convenient. The assembly instruction for extracting one, two, four, or longer bytes of data in the same manner as all other formats: first extracting the lowest bit byte at the offset address of 0, because the address offset and the number of bytes are a one-to-many relationship, and 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.

4.JAVA byte order

Big-endian, Little-endian are related to multibyte-type data, such as the Int,short,long type, but have no effect on single-byte data byte. Big-endian is low-byte emissions at the high end of memory, high-bit bytes emitted at the low end of memory. And Little-endian is the opposite.

such as int a = 0x05060708

In case of Big-endian, store as:

BYTE number 0 1 2 3

Data 05 06 07 08

In case of Little-endian, store as:

BYTE number 0 1 2 3

Data 08 07 06 05

5. Network byte order:

Coincidentally, all network protocols are also used in the big endian way to transfer data. So sometimes we also call the big endian way the network byte order. When two hosts communicate in different byte order, they must be converted into the network byte order before transmitting the data.

6.JAVA Example:

Let's first look at the results under Java default byteorder:

   Public Static void byteorderforinttest () {
  2:         bytebuffer buf =bytebuffer.allocate (10);
  3:         System.out.println ("
  4:         
  5:         buf.putint (1);
  6:         buf.putint (2);
  7:         
  8:         buf.flip ();
  9: For         (int i=0;i<buf.limit (); i++) {
Ten:             System.out.println (Buf.get ());
One:         }
:     }

The result of the execution is:

Then we'll modify the next Byteorder:

   Public static void Byteorderforinttest () {
  2:         bytebuffer buf =bytebuffer.allocate (10);
  3:         System.out.println ("
  4:         
  5:         Buf.order (Byteorder.little_endian);
  6:         System.out.println ("" +buf.order (). toString ());
  7:         
  8:         buf.putint (1);
  9:         Buf.putint (2);
10:         
One:         buf.flip ();
: For         (int i=0;i<buf.limit (); i++) {
:             System.out.println (buf.  Get());
:         }
:     }

The result of the execution is:

Resources:

1.http://baike.baidu.com/link?url=awodfcwi9mo7zg-kfk6uhfousphs4_-ohoivx-hg74e_ Dsqqbn8glpivtximsuelrylgdyjxg1hxhekfnudmpa2.http://baike.baidu.com/link?url=h235mhpfb3pn_dx_ke6cvkqkuxwdprmdlot6iuwjyp10vmgjnydgg_mn5lw1gc-bkarrxxpnnn_toql5ostqkk3.http://blog.csdn.net/sunshine1314/article/details/2309655

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.