Big-end and small-end affect the storage of bit domains and big-end Storage

Source: Internet
Author: User

Big-end and small-end affect the storage of bit domains and big-end Storage

struct Test{    unsigned short int a:5;//a1a2a3a4a5    unsigned short int b:5;//b1b2b3b4b5    unsigned short int c:6;//c1c2c3c4c5c6};

First arrange

Low address ---- high address

A1a2a3a4a5b1b2b3b4b5c1c2c3c4c5c6

For big ends

Low address storage is high, so the memory distribution is like this:

| A1a2a3a4a5b1b2b3 | b4b5c1c2c3c4c5c6 |

For small terminals

Store low-level bbs at a low-level address in a byte (for example, for B, low-level b4b5 is stored at a low-level address)

| B3b4b5a1a2a3a4a5 | c1c2c3c4c5c6 b1b2 |

First, in the first byte, bits are placed at the low bits of B.

Second, in a byte, B is arranged in front of a, c is arranged behind B, and B is in front of B.

I did an experiment on my local machine to verify the correctness:

int main(){    Test test;    test.a = 16;    test.b = 11;    test.c = 16;    int i = *(short*)&test;    cout<<i<<endl;    return 0;}

Let's take a look at the memory layout.

10000 01011 010000

01110000 | 01000001

Because it is a small end, the low address stores high bytes. Therefore, the value is 0x4170 and is converted to decimal 16752.


What is the difference between the big-end storage method and the Small-end storage method?

The large-end mode stores low-end data on the high address. High positions are stored on the address.
The small-end mode stores the status on the low-end address. High positions are stored on high addresses.

For example, the storage method of 16-bit 0x1234 in Little-endian mode CPU memory (assuming that it starts from address 0x4000) is:
Memory Address 0x4000 0x4001
Storage content 0x34 0x12
In Big-endian mode, the CPU memory is stored as follows:
Memory Address 0x4000 0x4001
Storage content 0x12 0x34

Big-end storage and small-end Storage

Large-end and small-end data are stored in the byte sequence. The so-called "Little Endian" is INTEL's mode. The low bytes of data are stored in the memory and low addresses, and the high bytes are stored in the high address, that is, the "high, low, and low" principle mentioned in X86. Byte3 Byte2 Byte1 Byte0 corresponds:

Base Address + 0 Byte0
Base Address + 1 Byte1
Base Address + 2 Byte2
Base Address + 3 Byte3

The so-called "Big Endian" is the mode adopted by MOTO. The low bytes of data are stored in the memory's high address, and the high bytes of data are stored in the memory's low address. Byte3 Byte2 Byte1 Byte0 corresponds:

Base Address + 0 Byte3
Base Address + 1 Byte2
Base Address + 2 Byte1
Base Address + 3 Byte0

Java uses the big-End sequence to store data. Big-endian: Low-byte data is stored in High-level memory. For example, for, 12 is high data, and 34 is low data, the storage format in java should be 12 low addresses with memory and 34 high addresses with memory. The storage format in x86 is the opposite.

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.