Big-endian mode and small-end mode

Source: Internet
Author: User

For program objects that span multiple bytes, we must establish two rules: what is the address of this object and how these bytes are arranged in memory. On almost all machines, multibyte objects are stored as contiguous sequence of bytes, with the address of the object being the smallest address of the bytes used. For example, suppose that the address of a type int variable x is 0x100, that is, the value of the address expression &x is 0x100. Then, the 4 bytes of x are stored in the 0x100,0x101,0x102 and 0x103 locations of the memory.

Arranging the bytes representing an object has two general rules. Consider a W-bit integer, where the bit is expressed as [Xw-1,xw-2,..., x1,x0], where xw-1 is the most significant bit I, and x0 is the least significant bit, assuming that W is a multiple of 8, the bits can be divided into bytes, where the most significant byte contains bits [Xw-1,xw-2,..., XW-8], the least significant byte contains the bit [x7,x6,..., x0], and the other bytes contain the middle bit. Some machines choose to store objects in memory in the order in which they are from the least significant byte to the most significant byte, while others are stored in the order from the most significant byte to the lowest valid byte. The first rule-the least significant byte in the front-of-the-way, called the small-end method (little endian). This rule is used by most Intel compatible machines. The latter rule-the most significant byte in the front of the way, called the big endian. Most IBM and Sun Microsystems machines use this rule.

Continuing our previous example, suppose the variable x is of type int, at address 0x100, with a hexadecimal value of 0x1234567. The address range is 0x100~0x103 bytes, and their order depends on the type of machine.

  

Note that in the word 0x01234567, the hexadecimal value of the high byte is 0x01, and the low byte value is 0x67.

 

How to judge big-endian mode and small-end mode

Method One

  

voidCheckendian () { ShortA =0x1234; Char* p = (Char*) &A; if((*p) = =0x12) {printf ("Big endian\n"); } Else if((*p) = =0x34) {printf ("Littleendian\n"); }}

Method Two:

voidCheckendian () {Union Num { ShortA; Charb;    } num; NUM.A=0x1234; if(Num.b = =0x12) {printf ("Big endian\n"); } Else if(Num.b = =0x34) {printf ("Little endian\n"); }}

Resources:

1. http://blog.csdn.net/ce123_zhouwei/article/details/6971544

Big-endian mode and small-end mode

Related Article

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.