For the following code
#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;
}
The output is
l1= 9223372036854775807
l2= -9223372036854775808
9223372036854775808
Why is l1
not long_min
and why is l2
? I've read that the string can contain +
/-
sign in the beginning for ato*,strto* functions. But negative numbers in binary don't have any -
sign in 2's complement that computer uses. I'm confused.