C interview questions (Judge big end and small end)

Source: Internet
Author: User

The recent status is terrible, and even the problems with big-end and small-end terminals are vague!

 

I do not usually make judgments on the big end and small end (by default, they are all small ends). After a long time, I just remember that the big end and small end are related to the eggs ^: ^

 

I. Concept of Large-end and small-end (the following concepts are from the Network)

 

The term Endian is written by Jonathan Swift. This Book classifies all people into two categories based on the way they open the eggs. From the beginning, the people who open the eggs are classified as Big Endian, from the beginning, the person who opens the eggs is classified as Littile Endian. The civil war in the minor people's country stems from the fight between Big-Endian and Little-Endian when eating eggs. Big Endian and Little Endian in the computer industry have also caused almost a war. In the computer industry, Endian indicates the order in which data is stored in memory. The following examples describe the differences between large and small-end models in computers.

If a 32-bit integer 0x12345678 is stored in an integer variable (int), the storage of this integer variable in the memory is shown in the following table. For the sake of simplicity, this book uses OP0 to represent the highest Byte MSB (Most Significant Byte) of a 32-bit data, and OP3 to represent the lowest Byte LSB (Least Significant Byte ).

 

 

Address offset

Big end Mode

Small-end Mode

0x00

12 (OP0)

78 (OP3)

0x01

34 (OP1)

56 (OP2)

0x02

56 (OP2)

34 (OP1)

0x03

78 (OP3)

12 (OP0)

 

If you store a 16-bit integer 0x1234 in a short INTEGER (short. The small integer variables are stored in the memory in the size mode as shown in the following table.

 

 

Address offset

Big end Mode

Small-end Mode

0x00

12 (OP0)

34 (OP1)

0x01

34 (OP1)

12 (OP0)

 

As shown in the preceding table, the main difference between data storage in the size mode is that the data is stored in byte order,The large-end mode stores the High Level in the low address, and the small-end mode stores the low level in the high address. Data storage in large-end mode is in line with human normal thinking, while data storage in Small-end mode is conducive to computer processing.. So far, data has been stored on large or small ends, and its advantages and disadvantages are still unknown.

 

Ii. Program judgment

 

# Include <stdio. h>

Union little_big_end
{
Short x16;
Long y32;
};

 

Int main (void)
{
Little_big_end LBE;

Printf ("the size of LBE = % d/N", sizeof (LBE ));

LBE. y32 = 0x12345678;

If (LBE. x16 = 0x1234)
{
Printf ("the memory format is big end! /N ");
}
Else if (LBE. x16 = 0x5678)
{
Printf ("the memory format is little end! /N ");
}

Getchar ();

Return 0;
}

 

Of course, you can directly store 0x12345678, and then read the low-address data for judgment. (Here, I will review the usage of shared objects)

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.