C learning the size of the end

Source: Internet
Author: User

Off-topic: if wrong, please leave a message to inform me.

The size end is the way the values are arranged in memory: the relationship between the high and low bits of the value and the size of the memory address.

Large/small end, English big/little Endian.

Note: Endian, is the byte order, byte storage order of meaning, also has the meaning of the mantissa. The meaning of the mantissa here is just right, for the following reasons.

First, a premise : The memory address of the first XX bit is usually the system reserved area for running kernel programs. So the user program starts at the other end---that's why the user program is stretching from the high address to the low address. (another may not be considered in this article)

In the above premise, we know that the length of the int type is 4 bytes, which means that it takes up 4 bytes of memory space.

So, how does it occupy?

with int i = 0x12345678; (see note ) For example, there can be two ways, one is 12 34 56 78, the address is reduced sequentially, the other is 78 56 34 12, which is the opposite of the preceding one.

Both ways can be understood. However, it is important to remember that the address of int is always low (note the premise). That is, &i gets the address that is always low.

So, in the first way, the address should be the space address of 78, and the second way, the address is the space address of 12!

Now can continue to say the mantissa (endian), the mantissa of the 0x12345678 is naturally 78, the number of the end if it is a low address, is the small end of the alignment, on the contrary, is the big-endian alignment!

Code Verification of size end alignment under Windows

#include <stdio.h>//test size End AlignmentintMainintargcChar Const*argv[]) {    intA=0x12345678; printf ("%p,%x\n", &A, a); Char*p= (Char*) &A; Char(pp=p; Char*p3=p; //Output Size End     for(inti =0; I <sizeofA ++i) {printf ("%p,%x\n", p++, *p); }    //How do I invert the size side?      for(inti =0; I <sizeof(a)/2; ++i) {Chartmp=*P2; *P2 = * (p2+sizeof(a)-1-2*i); * (p2+sizeof(a)-1-2*i) =tmp; ++P2; } printf ("After transfer:%p,%x\n", &A, a);  for(inti =0; I <sizeof(a)/2; ++i) {(*P3) ^= (* (p3+sizeof(a)-1-2*i)); (* (p3+sizeof(a)-1-2*i)) ^= (*p3); (*P3) ^= (* (p3+sizeof(a)-1-2*i)); ++P3; } printf ("After transfer again:%p,%x\n", &A, a); return 0;}
View Code

Notes :

Some people may not understand why this number is used, or why this number can explain the problem.

This is the basis of something:

intIs4bytes1 byteIs8Bits4bits is just a 16 binary number. 1 int==4bytes;1 byte==8bits;0x0~0xF==4Bits//(0000 ~ 1111)1 byte==0x00~0xFF;//(00000000 ~ 11111111)1 int==0x00 xx xx xx~0xFFFF FF FF;

So 1 byte is 2 16 binary digits, so int is 8 16 binary numbers!

To differentiate the contents of each byte, setting it to a different value is the best choice!

C learning the size of the end

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.