Large and small order

Source: Internet
Author: User

I. Overview

Byte order, Also knownClient order,Tail order, English:Endianness.

In the field of computer science,Byte orderIt refers to the order of bytes that store multi-byte data. A typical case is the storage mode of integers in the memory and the transmission sequence of network transmission. Endianness can also be usedOrder(BIT ).

The size-to-size sequence is related to the hardware architecture. All x86 series PCs are in the small-End sequence and have nothing to do with the operating system. The Solaris System on the x86 series PC is in small order, while the Solaris System on the sun or Linux platform is in large order.

Large-end byte order, high-byte storage in memory low-address, low-byte storage in memory high-address; Small-end byte order vice versa.

For example, a long data type is 0x12345678.
Large-end byte order:
Low memory address --> 0x12
0x34
0x56
High memory address --> 0x78

Small-end byte order:
Low memory address --> 0x78
0x56
0x34
High memory address --> 0x12

  1. Ii. Large and small orders

 

Big client Sequence(English: Big-Endian) orBig tail order.


Data in 8 bits:

Address growth direction

...

0x0a

0x0b

0x0c

0x0d

...

In the example, the highest valid bit (MSB, most significant byte) is0x0aStored at the lowest memory address. Next byte0x0bThere is a later address. It is similar to the reading order of hexadecimal bytes from left to right.

Data in 16 bits:

Address growth direction

...

0x0a0b

0x0c0d

...

 

 

The highest 16-bit unit 0x0a0b is stored at a low level.

Small client order(English: Little-Endian) orTail order.

Data in 8 bits:

Address growth direction →

...

0x0d

0x0c

0x0b

0x0a

...

The minimum valid bit (LSB, least significant byte) is0x0dStored at the lowest memory address. The next byte is followed by the address.

Data in 16 bits:

Address growth direction →

...

0x0c0d

0x0a0b

...

 

 

Minimum 16-bit unit0x0c0dLow storage.

The CPU with large-end order and CPU with small-end order are not only in bytes, but also in BIT order.
For example, 0x01 is stored in memory.
Large-End sequence: Memory low bit: 00000001 memory high bit
Small-End sequence: Memory low bit: 10000000 memory high bit

For example, 0x00000001
Large-End sequence: Memory Low Bit 00000000 00000000 00000000 00000001 memory high bit
Small-End sequence: Memory Low Bit 10000000 00000000 00000000 00000000 memory high bit

Iii. DETERMINATION METHOD

Typedef struct tagregion {unsigned char region1: 1; unsigned char region2: 6; unsigned char region3: 1;} region; Union {region BB; unsigned char AA;} cc; int main (INT argc, char * argv []) {CC. bb. region1 = 1; printf ("Hello world! % D/N ", CC. aa); // print 1 indicates a small end, and print 128 indicates a large end. Return 0 ;}

  

static unsigned char isBigEndian(){        const unsigned short test= 0xFF00;        return *((unsigned char *) &test);};

The following methods are more concise:

 1 #include <iostream>
2 using namespace std;
3
4 int main(int argc, char **argv)
5 {
6 short s = 0x0102;
7 if((*(unsigned char*)&s) == 2)
8 cout<<"little endian"<<endl;
9 else if((*(unsigned char*)&s) == 1)
10 cout<<"big endian"<<endl;
11 else
12 cout<<"unknown endian\n"<<endl;
13
14 return 0;
15 }

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.