C++ Primer 答客問 (39) - little endian, big …

看板CompBook (電腦用書)作者時間24年前 (2000/03/25 22:48), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
C++ Primer 答客問 (39) - little endian, big endian 侯捷 jjhou@ccca.nctu.edu.tw 2000.03.25 第一次發表於 清大.楓橋驛站(140.114.87.5).電腦書訊版(Computer/CompBook) 本文將於日後整理於 侯捷網站/侯捷譯作/C++ Primer 中文版/答客問 侯捷網站:www.jjhou.com ---------------------------------------------------------------- kylin 與我分享了一個看似奇怪的問題,是他的同事所寫的一段碼, 沒有註解,其結果令人丈二金剛摸不著頭腦。 大意是這樣:同事 A 所寫的一段程式碼中,將一份 DWORD 資料輸出 到 port 去,然後在讀入時以「chars 陣列」處理之。程式中運用了 DWORD 和 char[] 之間的型別轉換動作。奇怪的是同事 A 將 char 陣列 內容兩兩對換處理(這是奇怪之處)。舉例示範如下: // vc6[o] bcb4[o] g++[o] #include <iostream> using namespace std; typedef unsigned long DWORD; int main() { char szByteData[32] = "abcdefgh"; // 0x61,62,63,64,65,66,67,68 DWORD dwDwordData[5]; dwDwordData[0] = 0x41424344; // 'A','B','C','D' dwDwordData[1] = 0x45464748; // 'E','F','G','H' char* pPtrToByteData = (char*)dwDwordData; DWORD* dwPtrToDwordData = (DWORD*)szByteData; for (int i = 0; i < 8; i++) cout << pPtrToByteData[i]; // DCBA HGFE (顛倒了,所以同事 A // 才需做兩兩對換處理。此處未示範) cout << endl; cout << hex << "dwPtrToDwordData[0] = " << dwPtrToDwordData[0] << endl; // 64636261 (顛倒了) cout << hex << "dwPtrToDwordData[1] = " << dwPtrToDwordData[1] << endl; // 68676665 (顛倒了) } 初看實在令人訝異。後來我們才想到是 big endian 和 little endian 之故。 是的,這個程式在 Windows 9x 環境下跑,而 Windows 9x 的硬體平台 是 Intel x86,而 Intel x86 採用 little endian 的位元組排列模式, 也就是 "least significant byte appears first in the number". 所以當 DWORD 放到 Intel x86 系統的記憶體中,會 word 和 word 顛倒放置、byte 和 byte 顛倒放置。如果要以 bytes(chars)將它 一一讀入,必須自行顛倒各 bytes 的次序。 把這個程式拿到 FreeBSD on i386 上執行,結果與上相同。 但是如果把這個程式拿到 Solaris on Sparc 上執行,結果如下: for (int i = 0; i < 8; i++) cout << pPtrToByteData[i]; // ABCD EFGH(次序沒變) cout << endl; cout << hex << "dwPtrToDwordData[0] = " << dwPtrToDwordData[0] << endl; // 61626364(次序沒變) cout << hex << "dwPtrToDwordData[1] = " << dwPtrToDwordData[1] << endl; // 65666768(次序沒變) 因為 Sun Sparc 機器採用 big endian 的位元組排列模式,意即 "most significant byte is placed first"。 這給我們一個教訓: (1) 詭異的動作,卻不寫註解,害人害己。 (2) 移植性不高的程式碼,宜儘量避免 — 至少應有詳細說明。 順帶一提,很多人都不知道 big/little endian 的典故。 Microsoft Press Computer Dictionary 3/e p.49 說: "The term big endian is derived from Jonathan Swift's Gulliver's Travels, in which the Big-Endians were a group of people who opposed the emperor's decree that eggs should be broken at the small end before they were eaten." 一笑! -- the end  -- ※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ Mail: jjhou@ccca.nctu.edu.tw
文章代碼(AID): #utD6a00 (CompBook)
文章代碼(AID): #utD6a00 (CompBook)