当前位置:网站首页>Two methods to judge the storage of large and small end

Two methods to judge the storage of large and small end

2022-06-11 21:54:00 Code loving students

1. Coercive transformation

Given int Variable of type a, The assignment is 1,1 Of 16 Into the system for :00 00 00 01.

If the small end stores a Stored as :01 00 00 00.

Big end storage is :00 00 00 01.

You can take out a Your address is forced to char* type ,*(char*)&a To judge the value as 0/1.

If it is 0 It is big end storage , yes 1 Store for small end .

The code implementation is as follows :

int main()
{
	int a = 1;
	//00 00 00 01
	if (*(char*)&a == 1)
		printf(" Small end storage \n");
	else
		printf(" Big end storage \n");
	return 0;
}

2. Using Commons

Pictured :

We can find out a,b The address of is the same , So for b Changes , Will also change a. because b Occupy four bytes , and a There is only one byte .

  In the case of small end storage c In Chinese, it means 1,

On the contrary, if it is large-end storage, then c In Chinese, it means 0.

The code implementation is as follows :

typedef union A
{
	char a;
	int b;
}A;
int main()
{
	A a;
	a.b = 1;
	if (a.a == 1)
		printf(" Small end storage \n");
	else
		printf(" Big end storage \n");
}

原网站

版权声明
本文为[Code loving students]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112135378859.html