【发布时间】:2020-04-14 09:57:09
【问题描述】:
如下代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<64>b(numeric_limits<long>::min());
string s=b.to_string();
char *ptr=new char [s.size()+1];
strcpy(ptr,s.c_str());
long l1=strtol(ptr,NULL,2);
cout<<"l1= "<<l1<<endl;
long l2=strtoul(ptr,NULL,2);
cout<<"l2= "<<l2<<endl;
cout<<strtoul(ptr,NULL,2)<<endl;
}
输出是
l1= 9223372036854775807
l2= -9223372036854775808
9223372036854775808
为什么l1不是long_min,为什么是l2?我读过该字符串可以包含 +/- 在 ato*,strto* 函数开头的符号。但是二进制中的负数在计算机使用的 2 补码中没有任何 - 符号。我很困惑。
【问题讨论】:
-
errno 应该设置为 ERANGE,不要忘记检查它。 And this.