"Go" how to determine whether the CPU is big-endian or small-end mode

Source: Internet
Author: User

Original URL: http://blog.csdn.net/ysdaniel/article/details/6617458

How to tell if the CPU is big or small end mode http://blog.sina.com.cn/s/blog_66a61f310100i7m0.html

Msb:most significant bit-------most significant bit
Lsb:least significant bit-------least significant bit

Big-endian mode (Big-edian)
The BIG-ENDIAN:MSB is stored on the lowest-end address.

For example, the double-byte number 0x1234 exists in the Big-endian way in the start address 0x00002000:

| Data |<--Address
| 0x12 |<--0x00002000
| 0x34 |<--0x00002001

In Big-endian, the sequence number in the bit sequence is as follows (in the case of double-byte 0x8b8a):

----+---------------------------------------------------+
bit | 00 01 02 03 04 05 06 07 | 08 09 10 11 12 13 14 15 |
----+MSB---------------------------------------------lsb+
Val |  1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 |
----+---------------------------------------------------+
= 0x8b8a

Small end mode (Little-endian)

The LITTLE-ENDIAN:LSB is stored on the lowest-end address.

For example, the double-byte number 0x1234 exists in the Little-endian way in the start address 0x00002000:

| Data |<--Address
| 0x34 |<--0x00002000
| 0x12 |<--0x00002001

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 09 08 | 07 06 05 04 03 02 01 00 |
----+MSB---------------------------------------------lsb+
Val |  1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 |
----+---------------------------------------------------+
= 0x8b8a

c) Network byte order: TheTCP/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.

The network byte order is the "WYSIWYG" order. The byte order of the Intel type CPU is the opposite.

For example, the short b=0102h above (16 binary, each of which represents the width of a byte). What you see is "0102", according to General mathematical knowledge, the number of axes from left to right increase, that is, memory address from left to right increase, in memory this short B byte order is:

01 02

This is the network byte order. The order you see is consistent with the order in memory!

In the arm system, each word unit contains 4 byte units or two half-character units. In the word cell, the 4 bytes which is the high byte and which is the low byte have two different formats: Big-endian and Little-endian. In the small-end mode, the lower byte is placed at the low address, the high byte is placed at the higher address, and in the big-endian mode, the low byte is placed at the high address and the high byte at the low address.

In the C language, different types of variables in the common Body (union) are stored in the same paragraph, unlike structs. Using this feature, it is possible to determine whether the storage system is a big-endian or small-end mode in an arm or x86 environment using a union variable.

#include "stdio.h"
int main ()
{
Union W
{
int A; 4 bytes
Char b; 1 byte
C
C.a=1;
if (c.b==1)
printf ("It is little_endian!\n");
Else
printf ("It is big_endian!\n");
return 1;
}
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 According to the C.B value of the situation can determine the CPU mode, now XP environment of the Intel CPU is a small terminal mode, do not believe you can test!

It is true to judge whether a system is a big head order or a small-endian method.
Like what:
-begin-
int x=1; 0x00000001
if (* (char*) &x) {
/* Little endian. Memory Image 01 00 00 00 */
}else{
/* big endian. Memory Image 00 00 00 01 */
}
-end-
Memory data (0x00000001)-〉/small head Fu She-〉 value (1)
The prerequisite for discrimination here is to know the memory data and the corresponding values, so you can determine the size of the head word Fu She.

See also: http://www.cnblogs.com/wxxweb/archive/2011/06/28/2092108.html
Reference: Size End conversion function http://blog.csdn.net/ysdaniel/article/details/6772392

"Go" how to determine whether the CPU is big-endian or 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.