In: Computer Science
If you have any doubts, please give me comment...
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
bool convert2(int & n, const string & bits);
int main(){
int n;
cout<<"returns: "<<convert2(n, "10011")<<endl;
cout<<"n = "<<n<<endl;
return 0;
}
bool convert2(int & n, const string &bits){
int n_bits = 5;
int i=0;
int result = 0;
for(int i=1; i<n_bits; i++){
if(bits[i]!='1' && bits[i]!='0')
return false;
if(bits[i]=='0')
result += pow(2, n_bits-i-1);
}
result += 1;
if(bits[0]=='1')
result = -(result);
n = result;
return true;
}