[C] Determine whether the current system is big endian or little endian

1. Introduction of big and small end

  In effect, endianness refers to how data is stored in memory.

Big-endian (storage mode) : The contents of the low-endian byte order of a data are stored at the high address, and the contents of the high-order byte order are stored at the low address.

Little-endian byte order (storage mode) : The content of the high-order byte order of a data is stored at the high address, and the content of the low-order byte order is stored at the low address.

2. Determine whether the current system is big endian or little endian

  int a = 1;

  Assuming that there is a number a = 1, draw the memory distribution of a in big-endian and little-endian cases respectively.

a. Big endian

                             

b. Little endian

                              

  In fact, we only need to judge whether the low address is 1 or not. When the current system is in big-endian storage mode, 00 is stored at its lower address, and when the current system is in little-endian storage mode, 01 is stored at its lower address. Then this problem can be simplified as only judging whether the content of a byte is 1, and the current number is an integer type, and the type of the number can be cast to char type here.

/*
* Function name: JudgeSystem
*
* Function: determine whether the current system is big endian or little endian
*
* Entry parameter: void
*
* Exit parameter: 0 or 1
*
* return type: int
*/

int JudgeSystem(void)
{
	int a = 1;
	char * p = (char *)&a;

	if (1 == *p)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

  Disadvantage: The code is cumbersome, in fact, the content of *p is either 1 or 0.

  Optimization: return *p directly.

/*
* Function name: JudgeSystem
*
* Function: determine whether the current system is big endian or little endian
*
* Entry parameter: void
*
* Export parameter: *p
*
* return type: int
*/

int JudgeSystem(void)
{
	int a = 1;
	char * p = (char *)&a;

	//return 1 if it is little endian, 0 if it is big endian
	return *p;
}

  Insufficiency: Pointer variable p is created.

  Optimization: Return *(char *)&a directly.

/*
* Function name: JudgeSystem
*
* Function: determine whether the current system is big endian or little endian
*
* Entry parameter: void
*
* Export parameters: *(char *)&a
*
* return type: int
*/

int JudgeSystem(void)
{
	int a = 1;

	//return 1 if it is little endian, 0 if it is big endian
	return *(char *)&a;
}

  Introduce an ingenious method, using the union to achieve,

int i;
char c;
  i and c share a 4-byte memory, as shown in the figure below,


  In this way, i will change when c is changed, and c will also change when i is changed.

/*
* Function name: JudgeSystem
*
* Function: determine whether the current system is big endian or little endian
*
* Entry parameter: void
*
* Export parameter: un.c
*
* return type: int
*/

int JudgeSystem(void)
{
	unity one
	{
		char c;
		int i;
	};

	union One one;
	un.i = 1;

	//return 1 if it is little endian, 0 if it is big endian
	return un.c;
}

  main function

#define _CRT_SECURE_NO_WARNINGS 1

/*
* Copyright (c) 2018, code farmer from sust
* All rights reserved.
*
* File name: JudgeSystem.c
* Function: determine whether the current system is big endian or little endian
*
* Current version: V1.0
* Author: sustzc
* Completion date: April 30, 2018 19:17:25
*/

# include <stdio.h>

int main(void)
{
	int ret = JudgeSystem();

	if (1 == ret)
	{
		printf("The system is little endian!\n");
	}
	else
	{
		printf("The system is big endian!\n");
	}

	return 0;
}

  output result

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325388345&siteId=291194637