C program for judging the size and order

Source: Internet
Author: User

C program for judging the size and order
Concept

Large-end format: the high bytes of word data are stored in the low address, while the low bytes of word data are stored in the high address.
Small-end format: In contrast to the large-end storage format, the low-end storage format stores the low bytes of word data, and the high-end storage stores the high bytes of word data.

For example5A6CMedium5AIs high byte,6CIs a low byte. In a large-order machine, the high byte is located in the low address, that is5A6CStorage, according6C5AStorage.

Why does this mode exist?

In computer systems, we use bytes. Each address unit corresponds to one byte.8bit. HoweverCBesides8bitOfcharIn addition16bitOfshortType,32bitOflongType (depending on the specific compiler ).8Bit processors, such16Bit or32Because the register width is greater than one byte, there must be a problem if multiple bytes are arranged. Therefore, the large-end storage mode and the Small-end storage mode are created.

Code Implementation

Please write oneCFunction. If the processor isBig_endianIs returned.0IfLittle_endianIs returned.1

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

Analysis:

ConsortiumunionThe storage order isAll members are stored from low addresses..

Short int x; char x0, x1; x = 0x1122; x0 = (char *) & x) [0]; // low address unit x1 = (char *) & x) [1]; // high address unit x 0 = 0x11 indicates a large end; if x0 = 0x22, it is a small client ......

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.