gpt4 book ai didi

c - C 中的 uint64_t 充当 uint32_t

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:32 25 4
gpt4 key购买 nike

我正在尝试获取一个 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)。

关于c - C 中的 uint64_t 充当 uint32_t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40310520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com