如何测试大端存储和小端存储
生活随笔
收集整理的這篇文章主要介紹了
如何测试大端存储和小端存储
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*1. 利用不同的數據類型占用空間不一樣測試*/
#include <stdio.h>
int main()
{
? ? x = 0x1122;
? ? x1 = ((char *)&x)[0]; ? ?//低地址
? ? x2 = ((char *)&x)[1]; ? ?//高地址
? ? printf("x1=%x\n",x1);
? ? printf("x2=%x\n",x2);
? ? return 0;
}
//
/*2.***************************************/
typedef unsigned char BYTE;
int main(int argc, char* argv[])
{
unsigned int num,*p;
p = #
num = 0;
*(BYTE *)p = 0xff;
if(num == 0xff)
{
printf("The endian of cpu is little\n");
}
else //num == 0xff000000
{
printf("The endian of cpu is big\n");
}
return 0;
}
/*3.*****************************************/
/*利用指針取值時不同類型讀取字節數不一樣*/
#include<stdio.h>
int main()
{
? ? int x = 1;
? ? char *p = (char *)&x;
? ? if(*p)
? ? {
? ? ? ? printf("little\n");
? ? }
? ? else
? ? {
? ? ? ? printf("large\n");
? ? }
? ? return 0;
}
/*4.*****************************************/
/*利用union*/
int checkCPU()
{
? {
union w
{
?int a;
?char b;
? ? } c;
c.a = 1;
return (c.b == 1);
? }
}
/*5.*******************************************/
/*神級代碼kernel/arch/arm/kernel/setup.c */
/*
static union {char c[4]; unsigned long mylong;}
endian_test={{'l', '?','?','b'}};
#define ENDIANNESS ?((char)endian_test.mylong);
*/
static union?
{?
char c[4];?
unsigned long mylong;?
} endian_test = {{ 'l', '?', '?', 'b' } };
#define ENDIANNESS ((char)endian_test.mylong);
Linux 的內核作者們僅僅用一個union 變量和一個簡單的宏定義就實現了一大段代碼同樣的功能!由以上一段代碼我們可以深刻領會到Linux 源代碼的精妙之處!(如果ENDIANNESS=’l’表示系統為little endian,
為’b’表示big endian )
??
#include <stdio.h>
int main()
{
? ? short int x;
? ? char x1,x2;? ? x = 0x1122;
? ? x1 = ((char *)&x)[0]; ? ?//低地址
? ? x2 = ((char *)&x)[1]; ? ?//高地址
? ? printf("x1=%x\n",x1);
? ? printf("x2=%x\n",x2);
? ? return 0;
}
//
/*2.***************************************/
typedef unsigned char BYTE;
int main(int argc, char* argv[])
{
unsigned int num,*p;
p = #
num = 0;
*(BYTE *)p = 0xff;
if(num == 0xff)
{
printf("The endian of cpu is little\n");
}
else //num == 0xff000000
{
printf("The endian of cpu is big\n");
}
return 0;
}
/*3.*****************************************/
/*利用指針取值時不同類型讀取字節數不一樣*/
#include<stdio.h>
int main()
{
? ? int x = 1;
? ? char *p = (char *)&x;
? ? if(*p)
? ? {
? ? ? ? printf("little\n");
? ? }
? ? else
? ? {
? ? ? ? printf("large\n");
? ? }
? ? return 0;
}
/*4.*****************************************/
/*利用union*/
int checkCPU()
{
? {
union w
{
?int a;
?char b;
? ? } c;
c.a = 1;
return (c.b == 1);
? }
}
/*5.*******************************************/
/*神級代碼kernel/arch/arm/kernel/setup.c */
/*
static union {char c[4]; unsigned long mylong;}
endian_test={{'l', '?','?','b'}};
#define ENDIANNESS ?((char)endian_test.mylong);
*/
static union?
{?
char c[4];?
unsigned long mylong;?
} endian_test = {{ 'l', '?', '?', 'b' } };
#define ENDIANNESS ((char)endian_test.mylong);
Linux 的內核作者們僅僅用一個union 變量和一個簡單的宏定義就實現了一大段代碼同樣的功能!由以上一段代碼我們可以深刻領會到Linux 源代碼的精妙之處!(如果ENDIANNESS=’l’表示系統為little endian,
為’b’表示big endian )
??
總結
以上是生活随笔為你收集整理的如何测试大端存储和小端存储的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言register关键字——最快的关
- 下一篇: 函数、指针、数组的组合 及结构体和共用体