繁体   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