2016-10-28 95 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'的实现变化。 –