日本a√视频在线,久久青青亚洲国产,亚洲一区欧美二区,免费g片在线观看网站

        <style id="k3y6c"><u id="k3y6c"></u></style>
        <s id="k3y6c"></s>
        <mark id="k3y6c"></mark>
          
          

          <mark id="k3y6c"></mark>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 解析大端模式和小端模式

          解析大端模式和小端模式

          作者: 時間:2012-07-25 來源:網(wǎng)絡(luò) 收藏

          一、概念及詳解

          本文引用地址:http://yuyingmama.com.cn/article/148717.htm

          在各種體系的計算機中通常采用的字節(jié)存儲機制主要有兩種: big-endian和little-endian,即和小端

          先回顧兩個關(guān)鍵詞,MSB和LSB:

          MSB:MoST Significant Bit ------- 最高有效位

          LSB:Least Significant Bit ------- 最低有效位

          (big-edian)

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

          舉例,雙字節(jié)數(shù)0x1234以big-endian的方式存在起始地址0x00002000中:

          | data |-- address

          | 0x12 |-- 0x00002000

          | 0x34 |-- 0x00002001

          在Big-Endian中,對于bit序列中的序號編排方式如下(以雙字節(jié)數(shù)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 |

          +--------------------------------------------+

          = 0x8 B 8 A

          小端模式(little-endian)

          little-endian:LSB存放在最低端的地址上。

          舉例,雙字節(jié)數(shù)0x1234以little-endian的方式存在起始地址0x00002000中:

          | data |-- address

          | 0x34 |-- 0x00002000

          | 0x12 |-- 0x00002001

          在Little-Endian中,對于bit序列中的序號編排和Big-Endian剛好相反,其方式如下(以雙字節(jié)數(shù)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 |

          +---------------------------------------------+

          = 0x8 B 8 A

          二、數(shù)組在小端情況下的存儲:

          以unsigned int value = 0x12345678為例,分別看看在兩種字節(jié)序下其存儲情況,我們可以用unsigned char buf[4]來表示value:

          Big-Endian: 低地址存放高位,如下:

          高地址

          ---------------

          buf[3] (0x78) -- 低位

          buf[2] (0x56)

          buf[1] (0x34)

          buf[0] (0x12) -- 高位

          ---------------

          低地址

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

          高地址

          ---------------

          buf[3] (0x12) -- 高位

          buf[2] (0x34)

          buf[1] (0x56)

          buf[0] (0x78) -- 低位

          --------------

          低地址

          三、大端小端轉(zhuǎn)換方法:

          Big-Endian轉(zhuǎn)換成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))

          四、大端小端檢測方法:

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

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

          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*/



          關(guān)鍵詞: 模式 大端 解析

          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉