2016-10-28 37 views
0

我試圖得到一個數字是16位數字長0x1122334455667788和位移它來說明小端編號。uint64_t在C充當uint32_t

使用下面的示例代碼從存儲單元加載數

void endianCompute(memory_t memory, uint64_t start, uint64_t *save, int size){ 
    *save = 0; 
    for(int i = 0; i < size; i++){ 
     *save += memory[start + i] << (i)*8; 
     printf("add 0x%x\n", memory[start + i] << (i)*8); 
     printf("save 0x%x\n", *save); 
    } 
} 

的輸出產生:

save 0x88 
add 0x7700 
save 0x7788 
add 0x660000 
save 0x667788 
add 0x55000000 
save 0x55667788 
add 0x44 
save 0x556677cc 
add 0x3300 
save 0x5566aacc 
add 0x220000 
save 0x5588aacc 
add 0x11000000 
save 0x6688aacc 

這對我來說很有意義,直到加0×44,爲什麼位轉移不會繼續將數字推向左側?爲什麼我無法輸入過去8位數字的號碼?

+2

什麼是'memory_t'?另外,你的'save'行會在你的'add'行之前打印出來? – yano

+4

爲了打印'uint64_t',你需要一個不同的格式說明符,比如'%llx'。 –

+2

打開編譯器警告。 – Schwern

回答

3

打開編譯器警告,可能與-Wall,並顯示問題。

cc -Wall -g test.c -o test 
test.c:10:27: warning: format specifies type 'unsigned int' but the argument has type 'uint64_t' 
     (aka 'unsigned long long') [-Wformat] 
    printf("save 0x%x\n", *save); 
        ~~  ^~~~~ 
        %llx 

您需要使用%llx來打印64位整數(long long int)。

+2

使用'stdint.h'中的'PRIx64'宏提供字段代碼:'printf(「save 0x」PRIx64「\ n」,* save);''會更方便。這說明了哪種類型對應於'uint64_t'的實現變化。 –