Review the big end and Small End

Source: Internet
Author: User

I have talked about it in the Assembly class before, but I found that it is not enough... The following describes both the big end and small end.

 

Big end and Small End history:

Generally, all the eggs are eaten at the end of the big one, but once the king of a country broke his finger when he typed the eggs. Then, he ordered that all the eggs would be beaten from an early stage.

In order to satirize this issue, we have the concepts of big end and small end.

 

If an object is stored continuously across multiple bytes, the minimum address is the address of the object.

For example, an int is 4 bytes in 32-bit and 64-bit machines. If the object address is 0x100, the entire int occupies 0x103, 0 x, 0 x.

We all know the & Symbol. If int X; & X is the address of X, that is, 0x100.

[, 29,...] indicates the number of valid digits. The maximum valid bits is 31, and the minimum valid bits is 0.

Small-end method: the lowest valid bit exists at the beginning.

0x100 0x101

Bytes ---------------------------------------------------------------------------------------------------

|, |, | ....

Bytes ----------------------------------------------------------------------------------------------------

Big-end method: the most effective bit exists at the beginning.

0x100 0x101

Bytes ---------------------------------------------------------------------------------------------------

|, |, | ....

Bytes ----------------------------------------------------------------------------------------------------

The comparison between the two images shows the obvious difference, for example, int x = 2; convert to hexadecimal 0x00 00 00 02;

The storage sequence of the small end is:

0x100 0x101 0x102 0x103

----------------------------------------------------

| 02 | 00 | 00 | 00 |

----------------------------------------------------

As mentioned above, & X gets 0x100. Generally, programmers cannot see the difference in byte order.

In linux32, the result is small-end storage.

The following code is used to detect large-end and small-end code,

Remember ***: To detect large and small ends, you must convert them to unsigned char * Ch. Because this ch [I] is a byte.

 

Bytes --------------------------------------------------------------------------------------------------------------------

# Include <stdio. h> <br/> typedef unsigned char * byte_pointer; <br/> void show_bytes (byte_pointer start, int Len) <br/>{< br/> int I; <br/> for (I = 0; I <Len; I ++) <br/> printf ("%. 2x ", start [I]); <br/> printf ("/N "); <br/>}</P> <p> void show_int (INT X) <br/>{< br/> show_bytes (byte_pointer) & X, sizeof (INT); <br/>}< br/> void show_float (float X) <br/>{< br/> show_bytes (byte_pointer) & X, sizeof (float); <br/>}< br/> void test () <br/>{< br/> int val = 0x87654321; <br/> byte_pointer valp = (byte_pointer) & val; <br/> show_bytes (valp, 4 ); <br/> printf ("/N"); <br/> int val1 = 3510593; <br/> float val2 = 3510593.0; <br/> show_int (val1 ); <br/> show_float (val2); <br/>}</P> <p> int main () <br/>{< br/> test (); <br/> return 0; <br/>}< br/>

Int x = 0x12345678;

Unsigned char * val = (unsigned char *) & X; // the address of X.

Val [0] indicates the byte only to which the Val address points.

 

 

 

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.