簡體   English   中英

C中的uint64_t充當uint32_t

[英]uint64_t in C acting as a uint32_t

我正在嘗試獲取一個16位數字長0x1122334455667788的數字,並將其移位以解決小端編號問題。

使用以下示例代碼從存儲單元中加載數字

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

這對我來說一直很有意義,直到加0x44,為什么位移不繼續將數字向左推? 為什么我不能輸入超過8位的數字?

可能使用-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)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM