判断大小端程序

#include 

union test
{
	short val;
	char ch[sizeof(short)];
};

int main()
{
	union test t;

	t.val = 0x0102;

	if (t.ch[0] == 1 && t.ch[1] == 2)
	{
		printf("大端字节序\n");
	}
	else if (t.ch[0] == 2 && t.ch[1] == 1)
	{
		printf("小端字节序\n");
	}

	return 0;
}

#include 

int main()
{
	int a = 1;

	printf("%d\n", ((a & 0x000000ff) << 24) |
	               ((a & 0x0000ff00) << 8)  |
				   ((a & 0x00ff0000) >> 8)  |
				    (a & 0xff000000) >> 24);
	return 0;
}

你可能感兴趣的:(判断大小端程序)