Cracking the coding interview--Q16.5

Hawstein | February 9, 2013

题目

原文:

Write a program to find whether a machine is big endian or little endian.

译文:

写一个程序判断一台机器是大端序还是小端序。

解答

关于什么是大端序和小端序,可参见维基百科: 字节序

判断程序如下:

1
2
3
4
5
6
7
#define BIG_ENDIAN 0
#define LITTLE_ENDIAN 1
int TestByteOrder(){
	short int word = 0x0001;
	char *byte = (char *) &word;
	return (byte[0] ? LITTLE_ENDIAN : BIG_ENDIAN);
}

全书题解目录:

Cracking the coding interview–问题与解答

全书的C++代码托管在Github上:

https://github.com/Hawstein/cracking-the-coding-interview

声明:自由转载-非商用-非衍生-保持署名 | 创意共享3.0许可证,转载请注明作者及出处
出处:http://hawstein.com/2013/02/09/16.5/