返回> 网站首页
[转载]little endian和big endian的概念、判定
yoours2012-05-09 14:28:50
简介一边听听音乐,一边写写文章。
little endian和big endian是表示计算机字节顺序的两种格式,所谓的字节顺序指的是长度跨越多个字节的数据的存放形式.
假设从地址0x00000000开始的一个字中保存有数据0x1234abcd,那么在两种不同的内存顺序的机器上从字节的角度去看的话分别表示为:
1)little endian:在内存中的存放顺序是0x00000000-0xcd,0x00000001-0xab,0x00000002-0x34,0x00000003-0x12
2)big endian:在内存中的存放顺序是0x00000000-0x12,0x00000001-0x34,0x00000002-0xab,0x00000003-0xcd
需要特别说明的是,以上假设机器是每个内存单元以8位即一个字节为单位的.
简单的说,ittle endian把低字节存放在内存的低位;而big endian将低字节存放在内存的高位.
现在主流的CPU,intel系列的是采用的little endian的格式存放数据,而motorola系列的CPU采用的是big endian.
1. bool isBigEnding(){
int i = 1;
return ((char)i) == 1;
}
2. bool isBigEnding(){
union {
int i;
char c;
}x={1};
return x.c == 1;
}
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3. bool isBigEnding{
union {
int i ;
struct {
unsigned short int b : 8;
}bit;
}x={1};
return x.bit.b == 1;
}
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![[转载]little endian和big endian的概念、判定 - happyboy200032 - happyboy200032的博客](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
文章评论
1573人参与,0条评论