Storage of the "C + + language" int inside the computer

Source: Internet
Author: User

int occupies 4 bytes in a 32-bit computer, primarily to figure out the order in which the 4 bytes are stored in memory.

1#include <iostream>2 3 using namespacestd;4 5typedefstructInt_char6 {7     intA;8UnsignedChar*b;9 };Ten  One intMain () A { - Int_char A; -     inti; the     //test int; -A.a=0X01AB02CD; -a.b= (unsignedChar*) &a.a; -      for(i=0;i<4; i++) +printf"%02x\n", * (a.b+i)); -cout<<""<<Endl; +     //test short; A      ShortC=0x01ab; ata.b= (unsignedChar*) &C; -      for(i=0;i<2; i++) -printf"%02x\n", * (a.b+i)); -  - GetChar (); -     return 0; in}

The output is:

Cd

02

Ab

01

Ab

01

Thus, int,short are low-byte low address, high-byte existence high address;

How the 16-bit and 32-bit numbers are stored?—— "Http://see.xidian.edu.cn/cpp/html/1615.html"

In this link, there are now only two of the 32-digit storage: low-priority (Little-endian) and High-priority (Big-endian).

Which mode of storage is determined by the CPU, most computers store 32-bit numbers in high priority, but Intel CPU-based computers store 32-bit numbers in low-priority order .

My computer is using the CPU is Intel, which also confirms my previous results, first store the low byte to high byte in this order, that is, low byte existence low address, high byte existence high address .

Extended:

    1. Memory is so arranged that it can also be understood that when casting with type, the memory reads, such as int is cast to short, then short takes only two low bytes in int.
    2. How do you store "bits" in one byte of memory?

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

New questions:

0xAB is according to our normal thinking 1010 1011 storage, or 1101 0101 so in turn to store?

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.