Big-end and small-end Mode

Source: Internet
Author: User

1. Introduction

There are two methods to store data in the memory: Small-end mode and large-end mode. This depends on the CPU. The x86 CPU is generally in the small-end mode.

Little-Endian: stores low-order bytes at the starting address. (You only need to remember the small end to launch the large end. Both of them are confusing)

Big-edian: stores High-Order bytes at the starting address;

The terms "Big ends" and "small ends" indicate which end of the Multi-byte value is stored at the starting address.

For example, a two-byte short integer is to be stored.

Short a = 0x0102;

Small-end Mode

In the memory, it is as follows:

0x02
0x01

------------------------> Memory address increase direction.

VS:

This is the big-end mode;

0x01
0x02

-------------------------> Memory address increase direction.

2. Check the byte program of the current host:

#include <stdio.h> int main(){union{short s;char c[sizeof(short)];}un;un.s = 0x0102;if(sizeof(short) == 2){if(un.c[0] == 0x01 && un.c[1] == 0x02){printf("big-edian\n");}else if(un.c[0] == 0x02 && un.c[1] == 0x01){printf("little-edian\n");}else{printf("unknow\n");}}else{printf("sizeof(short) != 2,try other means!");}return 0;}

3. byte sequence Conversion

Different systems may use different modes, so that data interpretation errors may occur when multi-byte data is transmitted between different systems over the network. Fortunately, the network byte order (large-end mode) is defined ). When transmitting data over the network, we can convert the data from the local byte order to the network byte order and then send it to the network. After receiving data from the network, it is converted to the local byte sequence. In this way, we can avoid these bytecode details. Of course, conversion of byte order is not allowed when a single-byte string is transmitted.

Both windows and Linux platforms provide conversion functions. In Linux:

# Include <netinet/in. h> uint16_t htons (uint16_t host16bitvalue); uint32_t htonl (uint32_t host32bitvalue);/* returns the value of network byte order */uint16_t ntohs (uint16_t net16bitvalue); bytes ntohl (uint32_t net32bitvalue ); /* return the host's byte value */

16: indicates the value of 16bit; 32: indicates the value of 32bit;

S: Short integer, that is, 16bit; L: long integer, that is, 32bit;

H: local host byte; N: Network byte;

4. Advantages of big-end mode and small-end mode:

Small-end mode: you do not need to adjust the byte content for forced conversion of data. The storage methods of 1, 2, and 4 bytes are the same.
Large-end mode: the determination of the symbol bit is fixed to the first byte, and it is easy to judge positive and negative.

It seems that the investigation of the big-end and small-end model is a hot topic every year.

Reference link: Detailed description of big-end mode and small-end Mode

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.