Big end, Small End

Source: Internet
Author: User

Big end, Small End

Big-end format: in this 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. The small-end format is the opposite of the big-end storage format, in the small-end storage format, the low address stores the low byte of word data, and the high address stores the high byte of word data.

 
Next, if someone else gives you a question, you can write a simple program to test whether the current system is large-end storage or small-end storage. How can we solve this problem.

Here we can consider using two methods: Using pointers and using union Consortium;

First, we need to know the characteristics of union:

1. Multiple members can be defined in union. The size of union is determined by the maximum size of members.
2. union Members share the memory of the same block. Only one member can be used at a time.
3. assigning values to a member will overwrite the values of other members.
Let's look at a simple code:

    union  UN    {       char c;       int i;    }un;

  

In fact, the system allocates an int-sized space for us to store our Defined Characters c and I. The storage method is as follows:

 
We can see that c and I are a public space. Therefore, at the same time, we can only ensure that a variable member is used at a certain time.

The following are two small programs used to test whether the current system is large-end or small-end storage:

 
Method 1: Use Pointer features

# Include <stdio. h> # include <windows. h> int check_sys () {int a = 1; char * p = (char *) & a; if (* p = 1) return 0; else return 1 ;} int main () {int ret = 0; ret = check_sys (); if (ret = 0) printf ("little"); else printf ("big "); system ("pause"); return 0;} Method 2: Use the unionint check_sys () {union UN {char c; int I;} un; un. I = 1; if (un. c = 1) return 0; else return 1;} int main () {int ret = 0; ret = check_sys (); if (ret = 0) printf ("little"); else printf ("big"); system ("pause"); return 0 ;}

  

 

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.