Principles of Large-byte order and small-byte order and methods for determining Operating Systems

Source: Internet
Author: User

It is useless to say more, and the people around me are dizzy. You can see the address:

Large byte order: Put the High Effective bit in the low address segment. For example, in the byte addressing memory, store the value 0x0001 to the address 0x12345678.
Address Value
0x0004 0x78
0x0003 0x56
0x0002 0x34
0x0001 0x12

Small byte order: Put the low valid bit in the low address segment. For example, in the byte addressing memory, store the value 0x0001 to the address 0x12345678.
Address Value
0x0004 0x12
0x0003 0x34
0x0002 0x56
0x0001 0x78

Currently, the mainstream CPU and Intel series use the little endian format to store data.

 

Determination Method:

Many languages have built-in functions, but Linux hasn't found them yet. Write a function to judge it as follows:

 

Bool islittleendian ()
{
Union
{
Long long;
Char char [sizeof (long)];
} U;

U. Long = 1;
If (U. Char [0] = 1)
{
Return true;
}
Else if (U. Char [sizeof (long)-1] = 1)
{
Return false;
}
Else
{
Throw "unknown addressing! ";
}
}

 

The last size-byte sequence conversion function:

 

Void endianconvert (unsigned int * value)
{
* Value = * value <24 | (* value & 0xff00) <8) | (* value & 0xff0000)> 8) | (* value & 0xff000000)> 24 );
}

 

 

 

Refer:

Http://www.dnbcw.com/biancheng/c/esbu62056.html

Http://hi.baidu.com/wbwssb/blog/item/7114224df46c6dc6d1c86ac6.html

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.