2009年12月20日 星期日

MSB 與 LSB 的資料排列方式

假設資料結構子如下:
struct myData
{
unsigned addr_hi:2 // 佔 2 bits
unsigned instr :6 // 佔 6 bits
unsigned char instr_low // 佔 8 bits
}

資料內容為:
myData = 0x5A5B; // 0b0101101001011011

MSB表示方式
+-----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|value| 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |
+-----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+-----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|--------> 1 byte <--------- |--------> 1 byte <--------- |






LSB表示方式(定義struct的順序要調整過)
struct myData
{
unsigned char instr_low // 佔 8 bits
unsigned instr :6 // 佔 6 bits
unsigned addr_hi:2 // 佔 2 bits
}

+-----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|value| 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |
+-----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| bit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
+-----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|--------> 1 byte <--------- |--------> 1 byte <--------- |


小結:
MSB的機器在訂struct時要從bit 7 ~ bit 0
LSB的機器在訂struct時要從bit 0 ~ bit 7

沒有留言:

張貼留言