Use a C language program to determine the size-end Mode

Source: Internet
Author: User

1. The large-end mode stores low data points in the high address of the memory, while the high data points are stored in the low address of the memory. The opposite is true for the small-end mode.

2. Why are there big and small ends ???

In computer systems, storage is in bytes. Each address unit corresponds to one byte, with a byte equal to 8 bits. In C language, apart from 8-bit char, there are 16-bit short type and 32-bit long type (depending on the specific compiler ). For a processor whose digits are greater than 8 bits, such as a 16-bit or 32-bit processor, how to arrange storage of multiple bytes because the register width is greater than one byte, this comes with the big-end storage mode and small-end storage mode.

3. respective advantages:

Small-end mode: you do not need to adjust the byte content for forced conversion of data. The storage methods of 1, 2, and 4 bytes are the same.

Large-end mode: the determination of the symbol bit is fixed to the first byte, and it is easy to judge positive and negative.

4. the commonly used X86 structure is the small-end mode, while the KEIL C51 mode is the large-end mode. Many ARM and DSP are in small-end mode.

5. c language to determine the size-end Mode

Method 1:

Copy codeThe Code is as follows: void IsBigEndian ()
{
Short int a = 0x1122; // hexadecimal, a value occupies 4 digits
Char B = * (char *) & a; // convert the short (2 bytes) type to a single char byte, and B points to the starting byte of a (low byte)
If (B = 0x11) // The low byte stores the high byte data of the data.
{
// Large-end Mode
}
Else
{
// Small-end Mode
}
}

Method 2:

Copy codeThe Code is as follows: void IsBigEndian () // principle: the order in which union members are stored is that all members are stored from the low address, and all Members share the bucket.
{
Union temp
{
Short int;
Char B;
} Temp;
Temp. a = 0x1234;
If (temp. B = 0x12) // low bytes store high bytes of data.
{
// Large-end Mode
}
Else
{
// Small-end Mode
}
}

I verified on my machine that it was a 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.