久久婷婷香蕉热狠狠综合,精品无码国产自产拍在线观看蜜,寡妇房东在做爰3,中文字幕日本人妻久久久免费,国产成人精品三上悠亚久久

Hi,歡迎來到嵌入式培訓高端品牌 - 華清遠見教育科技集團<北京總部官網>,專注嵌入式工程師培養15年!
當前位置: > 華清遠見教育科技集團 > 嵌入式學習 > 講師博文 > 解析大端模式和小端模式
解析大端模式和小端模式
時(shi)間:2016-12-30作者:華清遠見

一(yi)、概(gai)念(nian)及詳解

在各種(zhong)體系的(de)計算機中(zhong)通(tong)常采用(yong)的(de)字節存儲機制主要有兩種(zhong): big-endian和(he)little-endian,即大端模(mo)式和(he)小端模(mo)式。

先回顧兩個關鍵詞,MSB和LSB:

MSB:Most Significant Bit ------- 高有效位
        LSB:Least Significant Bit ------- 低有效位

大端(duan)模式(big-edian)

big-endian:MSB存放在低端的地址上。

舉例,雙(shuang)字節數0x1234以big-endian的方式(shi)存在起(qi)始(shi)地(di)址0x00002000中:

| data |<-- address
        | 0x12 |<-- 0x00002000
 &nbsp;      | 0x34 |<-- 0x00002001

在Big-Endian中,對于bit序列中的序號編(bian)排方(fang)式(shi)如下(以雙字節(jie)數(shu)0x8B8A為例):

bit | 0 1 2 3 4 5 6 7 | 8 9 10 11 12 13 14 15
        ------MSB----------------------------------LSB
        val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 |
        +--------------------------------------------+
 &nbsp;      = 0x8 B 8 A

小端模式(little-endian)

little-endian:LSB存放在低端的(de)地址上。

舉例,雙字節數0x1234以(yi)little-endian的(de)方(fang)式存在起始地址0x00002000中:

| data |<-- address
        | 0x34 |<-- 0x00002000
         | 0x12 |<-- 0x00002001

在Little-Endian中(zhong),對于bit序(xu)列中(zhong)的(de)序(xu)號編(bian)排和(he)Big-Endian剛好(hao)相反(fan),其(qi)方式如下(以雙字節(jie)數(shu)0x8B8A為例):

bit | 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0
        ------MSB-----------------------------------LSB
        val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 |
        +---------------------------------------------+
 &nbsp;      = 0x8 B 8 A

二(er)、數組(zu)在大端小端情況下的存儲:

以unsigned int value = 0x12345678為例,分別看看在兩種字節序下其存儲情況,我們可以用unsigned char buf[4]來表示value:
Big-Endian: 低地(di)址(zhi)存放高位,如下:

高地址
        ---------------
        buf[3] (0x78) -- 低位
        buf[2] (0x56)
        buf[1] (0x34)
        buf[0] (0x12) -- 高位
        ---------------
    &nbsp;   低(di)地址

Little-Endian: 低地址存放低位,如(ru)下:

高地址
        ---------------
        buf[3] (0x12) -- 高位
        buf[2] (0x34)
        buf[1] (0x56)
        buf[0] (0x78) -- 低位
        --------------
     &nbsp;  低地(di)址

三、大(da)端小端轉換(huan)方(fang)法:

Big-Endian轉換成Little-Endian如下:

#define BigtoLittle16(A)                 ((((uint16)(A) & 0xff00) >> 8) | \
                                                                   (((uint16)(A) & 0x00ff) << 8))
        #define BigtoLittle32(A)                 ((((uint32)(A) & 0xff000000) >> 24) | \
                                                                   (((uint32)(A) & 0x00ff0000) >> 8) | \
                                                                   (((uint32)(A) & 0x0000ff00) << 8) | \
                                                                   (((uint32)(A) & 0x000000ff) << 24))

四、大(da)端小(xiao)端檢測方法(fa):

如何檢查處理器是big-endian還是little-endian?

聯合體(ti)union的存放(fang)順序是(shi)(shi)所有成員都從低地址開(kai)始存放(fang),利(li)用該特性就(jiu)可以輕松地獲得了CPU對內存采用Little-endian還是(shi)(shi)Big-endian模(mo)式讀寫。

int checkCPUendian()
        {
                union
                {
                        unsigned int a;
                        unsigned char b; 
                }c;
                c.a = 1;
                return (c.b == 1); 
        } 
        ;/*return 1 : little-endian, return 0:big-endian*/

發表評論
評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)