Big-endian parsing during data storage

First of all, big-endian storage refers to the mode in which individual data is stored in memory.

Table of contents

Understand big and small endianness

Code example

 Why are there big and small ends?

Design code to interpret your compiler.


 

Understand big and small endianness

Big-endian (mode) storage: refers to when data is stored in memory, ensuring that the low-order byte of the data is stored at the high address of the memory, and the high-order byte of the data is stored at the low address of the memory.

Little endian (mode) storage: Just the opposite of big endian, the low byte of data is stored at the low address of the memory, and the high byte of the data is stored at the high address of the memory.

Code example

#include<stdio.h>
int main()
{
	int a = 10;//存储一个整型,占4个字节
	//00000000 00000000 00000000 00001010	——10的二进制
	//整数以二进制补码存储在内存中,但为了便于观察,故用十六进制代替
	//四个二进制可以转换成一个十六进制
	//0x 0000 0000 0000 000a			——10的十六进制(前面的0x表示十六进制)
	//   高字节——————>低字节

	return 0;
}

Let’s use VS2022 to debug and take a look at the memory storage:

16933bf7d0514c08b8535e283bfa03d9.png

 From the compiled data, we know that the integer a occupies 4 bytes and is stored in the memory in hexadecimal format. The storage mode is based on the low-byte data being stored at the low address of the memory, and the high-byte data being stored in The high address of memory.

One additional point: a is an integer, occupies four bytes, and one byte (memory unit) occupies one address. What &a takes out is the low address in the memory.

 Why are there big and small ends?

In computer systems, we use bytes as units, and each address unit corresponds to a byte (8 bit). But there is not only one char type in the computer, but also types larger than one byte (short int long.. .) For example, in a 16-bit or 32-bit processor, since the width of the register is larger than one byte, there must be a problem of how to arrange multiple bytes. This leads to the big and small endian storage model.

For example : a 16-bit short type For big-endian mode, if 0x11 is placed at the low memory address (0x0010), then 0x22 should be placed at the high memory address (0x0011). Little-endian mode is coincidentally the opposite. The x86 we commonly use is little endian mode, while the KIEL C51 is big endian mode. Many ARP DSPs are in little-endian mode. Some ARM processors can also select big-endian or little-endian mode by hardware. Having said so much, just one sentence: big and small endianness depends on the hardware.

Design code to interpret your compiler.

Idea: First we want to determine the big and small endianness, so we must understand what is the difference between the big and small endian? : The storage order of single data in memory (then we can use this as a breakthrough)

Practical operation:

#include<stdio.h>
int main()
{
	int a = 1;
	//00 00 00 01	-———十六进制
	char* p = (char*)&a;//强制类型转换&a为char*类型以便拿到低地址字节

	//如果是大端:拿低位地址就是a的高字节数据0
	//如果是小端:拿低位地址就是a的低字节数据1
	if (*p == 1)
		printf("小端");
	else
		printf("大端");

	return 0;
}

 simplify:

#include<stdio.h>
int Check(int x)
{	
	return *(char*)&x;//强制类型转换为char*,返回低地址数据
}
int main()
{
	int a = 1;
	if (Check(a) == 1)
		printf("小端");
	else
		printf("大端");

	return 0;
}

In fact, the simplification is just a little: char* p=(char*)&a; *p=*(char*)&a;

                                      

                                                                          

 

 

Guess you like

Origin blog.csdn.net/C_Rio/article/details/129051229