对于以下代码
#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 的补码中没有任何符号。我很困惑。