全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1145
推到 Plurk!
推到 Facebook!

記憶體擺放問題

答題得分者是:syntax
yshihyu
一般會員


發表:23
回覆:18
積分:8
註冊:2006-11-28

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-01-31 00:46:51 IP:203.70.xxx.xxx 訂閱

[code cpp]
#include
typedef union DATA_TYPE_S {
unsigned short int i;
struct { unsigned char
B0:1,
B1:1,
B2:1,
B3:1,
B4:1,
B5:1,
B6:1,
B7:1,
B8:1,
B9:1,
B10:1,
B11:1,
B12:1,
B13:1,
B14:1,
B15:1;
} BIT;
}DATA_TYPE_S;

int main(int argc, char *argv[])
{
int i;
DATA_TYPE_S test;
test.i = 0x1234;

for ( i = 15; i >= 0; i--)
printf("%d", (test.i >> i) & 1);

printf("\n");
test.BIT.B0 = 1;
for ( i = 15; i >= 0; i--)
printf("%d", (test.i >> i) & 1);
printf("\n");
system("PAUSE");
return 0;

}

[/code]


值 0x1234 照 CPU 是 little endian 應該擺成 0x34 0x12

不過照上面 code 將B0 = 1; 是改到 0x34 的 ( 0011 010'0' ) 最後面那 bit

那不是應該是 0x43 0x21 擺到記憶體? 還是我誤解了

麻煩知道朋友解釋一下

謝謝
編輯記錄
yshihyu 重新編輯於 2008-01-31 00:51:38, 註解 無‧
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-01-31 09:51:58 IP:61.64.xxx.xxx 訂閱
你不是資工本科的喔!

我也不是,呵呵

0x1234 =
0.....0....1....0........1....0....0..0..........0..0..1..1.......0..1..0..0
15..14..13..12.....11..10..9..8..........7..6..5..4.......3..2..1..0

= 0x2^15 0x2^14 1x2^13 ......... 1x2^2 0x2^1 0x2^0

B0 當然是最後一個

目前為止,還沒有看到是用反過來的方式,例如
0x1234 = 0x2^(15 - 0) 0x2^(15 - 1) 1x2^(15 - 2) ......... 1x2^(15 - 13) 0x2^(15 - 14) 0x2^(15 - 15)

===================引 用 yshihyu 文 章===================

[code cpp]
#include
typedef union DATA_TYPE_S {
unsigned short int i;
struct { unsigned char
B0:1,
B1:1,
B2:1,
B3:1,
B4:1,
B5:1,
B6:1,
B7:1,
B8:1,
B9:1,
B10:1,
B11:1,
B12:1,
B13:1,
B14:1,
B15:1;
} BIT;
}DATA_TYPE_S;

int main(int argc, char *argv[])
{
int i;
DATA_TYPE_S test;
test.i = 0x1234;

for ( i = 15; i >= 0; i--)
printf("%d", (test.i >> i) & 1);

printf("\n");
test.BIT.B0 = 1;
for ( i = 15; i >= 0; i--)
printf("%d", (test.i >> i) & 1);
printf("\n");
system("PAUSE");
return 0;

}

[/code]


值 0x1234 照 CPU 是 little endian 應該擺成 0x34 0x12

不過照上面 code 將B0 = 1; 是改到 0x34 的 ( 0011 010'0' ) 最後面那 bit

那不是應該是 0x43 0x21 擺到記憶體? 還是我誤解了

麻煩知道朋友解釋一下

謝謝
系統時間:2024-05-19 4:27:51
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!