Big endian and Little endian

Today read an article on vnexpress, the title is quite a guest question:

http://vnexpress.net/tin-tuc/giao-duc/nhung-cau-hoi-phong-van-tuyen-dung-ky-quac-cua-apple-3515701.html

Personally, I find that the questions in them have many strange sentences and are not practical, but there is an interesting question:

  1. Write a code that allows you to detect the device you are running with little Endian or big Endian. (These two terms refer to differences in how to read and write data between computer platforms).

Since I don't know how to make this sentence, it should be google. After google, the following code is output:

Long does not touch C, so do not remember behavior when cast pointer, how to google, then find a very easy to understand answer on stackoverflow.

http://stackoverflow.com/a/12792301/983800

The idea of ​​that answer is, suppose we have 1 variable x with the value int 1.

With little endian, the memory layout of x will be as follows:

As for big endian, the memory layout of x will be:

Therefore, in order to know which type of architect is, it is only necessary to look at the first byte of x that is valid. Since x is int (4 bytes), in order to retrieve the first byte, we cannot use bit masking, because when using bit masking, the masked items also have the same endianess with x so the first byte will always be first.

The code above uses an interesting way rather than using pointer casting, casting from 1 pointer of the 4 byte type to the pointer of 1 byte type. Since the pointer still points to the first position (which will be seen on the image), if the value of this pointer is taken, the value of the first byte (according to the current endianess) will be available.

If you review the code, if (* (char *) & x) is the logic to check the value of the first byte, if it is 1, it will be little endian, if it is 0 it will be big endian

Digging a little further, why do two types of endian do anything for complexity?

According to wikipedia

Big endian is used mainly for network

Big-endian is most common format in data networking; fields in the protocols of the Internet protocol suite, such as IPv4, IPv6, TCP, and UDP, are downloaded in big-endian order. Không cho phép này, big-endian byte không đặt là được xác định vào byte byte máy .

Little endian is used mainly for CPU

Cảnh báo nhỏ-endian là chung cho Microprocessors, trong phần do không có significant influence trên microprocessor designs by Intel Corporation.

Listen also not very convincing, continue google then find the link on quora asking about the strengths of each type of endian weaknesses:

https://www.quora.com/What-are-the-advantages-of-using-Big-Endian-over-Little-Endian-in-a-computer-architecture

In the answers, the answer is quite convincing:

With big endian, the benefit is the same as human thought (the right is the smallest), so it is easy to debug.

Big endian's advantage:

Easier để người dùng để đọc khi kiểm tra giá trị bộ nhớ. Chỉ này này không thể áp dụng cho kết nối / giá trị Des dữ liệu khi được chuyển đổi với mạng

Không cần chuyển đổi cuối cùng khi khi gửi / lấy dữ liệu để / từ mạng

As for little endian, the advantage point is that it is easy to cast from a large type to a small type (for example, from 32 bits to 16 bits) because we do not need to calculate the smallest value bit in any position. In addition, little endian (seems) to benefit from calculating mathematics operation more.

Little endian's advantage:

  • Đang đọc được như là cùng giá giá trị khi đọc trong kích cỡ lớn hơn hoặc bằng giá trị. For example 20 = 0x14 if writing as một giá trị 64-bit vào bộ nhớ ở địa chỉ A sẽ được 14 00 00 00 00 00 00, và sẽ được đọc với 20 không dùng dùng 8, 16, 32, 64-bit đọc hoặc thực hiện đọc nào với chiều dài <= 64 tại địa chỉ A LIKE 24, 48 or 40 bits. Đây có thể được nâng cấp vào tùy chọn bên ngoài. In big-endian system, bạn cần biết trong đó, kích cỡ bạn đã được ghi rõ giá trị để đọc nó đúng, và để lấy ít nhất byte byte của bạn cần đọc trong byte A + n-1 (với n là một byte n + 1 (với n là một byte n + 1 (với n là byte trong n byte 1) của ghi này thay vào A. Đây thuộc tính này không thể tạo các giá trị để một kiểu nhỏ hơn như int32_t để không xác định giá trị Int16_t sẽ luôn các byte ở đầu đầu của số nguyên32.
  • Natural in computation. Cấu hình tác tử lớn hơn từ ít nhất một số nguyên lớn, so nó nhiều hơn để làm việc quá nhỏ. Không cho tập tin một 32-bit, nhưng có giới hạn bộ nhớ nào, chỉ 16 bit (như một số nguyên ARM) có thể với math với 16 bit trong một thời gian và đọc một halfword ở địa chỉ A, thêm nó thì đọc bản đầy đủ halfword at A + 2 và thêm thay vào thay thế để đọc hai đọc vào được thực hiện thêm thêm từ LSB. Đây là ít nhất sử dụng trong Arbitrary-xác thực arithmetic (hoặc bất kỳ việc hiện thời là hơn hơn sự kiến ​​trúc natural từ kích cỡ làm việc 64-bit maths trên 32-bit computers) vì nó sẽ được nhiều hơn khi cần đọc các số nguyên backius và các tác tử này.

ITZone via kipalog

Share the news now