Big-end and small-end judgment

Source: Internet
Author: User

Assume that the storage starts from the address 0x4000:

0x12345678 is also a 32-bit four-byte data, the maximum byte is 0x12, the lowest byte is 0x78:

In little-Endian mode, the CPU memory is stored as follows:

(High-byte, low-byte)

Memory Address

Zero X 4000

Zero X 4001

Zero X 4002

Zero X 4003

Store content

0x78

0x56

0x34

0x12

 In big-Endian mode, the CPU memory is stored as follows:

(The high byte is at the low address, and the low byte is at the high address)

Memory Address

Zero X 4000

Zero X 4001

Zero X 4002

Zero X 4003

Store content

0x12

0x34

0x56

0x78

As shown in the preceding table,

The main difference between data storage in the size mode is that the data is stored in the byte order. The large-end mode stores the data in the high-end mode and the Small-end mode stores the data in the high-end mode.

The use of big-end data storage is in line with human normal thinking, while the use of small-end data storage is conducive to computer processing. So far, data has been stored on large or small ends, and its advantages and disadvantages are still unknown.

Some processor systems store data in a small way, such as Intel's Pentium. Some processor systems use big-end data storage methods, such as IBM semiconductors and Freescale PowerPC processors. In addition to the processor, some peripherals are also designed to store data on large or small ends.

Question 1: Write a c function. If the processor is big_endian, 0 is returned. If the processor is little_endian, 1 is returned.

int checkCPU( ){       {                 union w                 {                            int a;                          char b;                 } c;                 c.a = 1;           return  (c.b ==1);       }}

Analysis:

The order in which union members are stored starts from the low address.

How does one store c. A on a small client?

Address
------------------------------------
| A + 1 | A + 2 | A + 3 | int;
| 0x01 | 0x00 | 0x00 | 0x00 |
-------------------------------------
| A | char B;
| 0x01 |
---------

How does one store c. A on a big end?

Address
------------------------------------------
| A + 1 | A + 2 | A + 3 | int;
| 0x00 | 0x00 | 0x00 | 0x01 |
------------------------------------------
| A | char B;
| 0x00 |
---------

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.