gpt4 book ai didi

c++ - unsigned long 大于 0xFFFFFFFF

转载 作者:太空狗 更新时间:2023-10-29 21:09:48 24 4
gpt4 key购买 nike

如果 unsigned long 最大值为 4294967295,测试程序如何得出 4294967297?

#include <iostream>
using namespace std;
unsigned long millis_diff(unsigned long then, unsigned long now) {
return now < then ? now + then : now - then;
}
int main()
{
unsigned long now = 2;
unsigned long then = 4294967295; //unsigned long maximum value
unsigned long diff = millis_diff(then, now);
cout << diff;
return 0;
}

最佳答案

原因是您的编译器似乎定义 unsigned long int 的方式与定义 unsigned long long int 的方式相同。:)

这是一个演示程序

#include <iostream>
#include <limits>

int main()
{
std::cout << "sizeof( unsigned long ) = " << sizeof( unsigned long ) << '\n';
std::cout << "The maximum values is " << std::numeric_limits<unsigned long>::max() << '\n';
}

它的输出是

sizeof( unsigned long ) = 8
The maximum values is 18446744073709551615

关于c++ - unsigned long 大于 0xFFFFFFFF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57044616/

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