由于某种原因,我不能得到4294967296或任何更大的数字存储在一个长。在第二次运行循环时,change变量在相乘后变为0,尽管它是一个长变量。
unsigned long interleave(unsigned int i1, unsigned int i2) {
long int out = 0l;
unsigned long r1;
unsigned long r2;
unsigned long change;
change = 1l;
unsigned long store;
unsigned long bitSize = 256l;
printf("%010x + %010x = ", i1, i2);
//printf("START CHANGE %016x or %lu\n", change, change);
for (int i = 3; i >= 0; i--) {
//Get remainder for int 1;
r1 = (unsigned long)i1 % (unsigned long)bitSize;
i1 = (unsigned long)i1 / (unsigned long)bitSize;
//Get remainder for int 2;
r2 = (unsigned long)i2 % (unsigned long)bitSize;
i2 = (unsigned long)i2 / (unsigned long)bitSize;
//printf("GOT %04x and %04x\n", r1, r2);
store = ((unsigned long)r1 * (unsigned long)bitSize) + (unsigned long)r2;
store = (unsigned long)store * (unsigned long)change;
//printf("STORE %016x\n", store);
change = ((unsigned long)change) * ((unsigned long)bitSize);
printf("CHANGE AFTER FIRST MULTIPLICATION: %016x or %lu\n", change, change);
change = ((unsigned long)change) * ((unsigned long)bitSize);
printf("CHANGE AFTER SECOND MULTIPLICATION: %016x or %lu\n", change, change);
//printf("OUT: %016x\n", out);
out += (unsigned long)store;
}
printf("%016x", out);
}
int main() {
interleave((unsigned)4294967295, (unsigned)2999999);
char* ch;
scanf("%c%c", ch, ch);
}
我试过将数据类型切换为“long long”或“long int”或“long long int”。似乎没有一个工作。我现在已经明确地将所有athemic转换为“unsigned long”,但这也失败了。该函数应该在每个字节的基础上交织两个整数,因此“c245a1”和“1e67ba”应该交织到“c21e4567a1ba”。相反,由于某种原因,当我使用长它不能容纳任何大的,也不能一个“长长”。
2条答案
按热度按时间jjjwad0x1#
感谢Barmar和dbush。原来我的系统上的长整型和整型一样大,我需要像Barmar建议的那样把它改成Long Long。然而我没有意识到这已经解决了问题,因为我需要在printf中使用“llx”而不是“x”,以便按照dbush的建议查看其余的数字。如果其他人也有这种问题,只需将您的长时间更改为长时间,看看是否可以修复它,并检查是否正确打印。
vhmi4jdf2#
C标准仅指定类型所需的最小值,不保证
long
将大于int