I want to create some functions that can take a number and convert it to binary, and vise versa.
The function for the 'long to binary' is this:
Consider that I have a char *_intBits[32] declared in the class that contains this function among others.
and the function of the binary to int is this:
But I am experiencing some problems. The result of the first function, when entered in the second function, it does not give me the proper result. I cannot find the mistake... Can anyone help?
The function for the 'long to binary' is this:
Consider that I have a char *_intBits[32] declared in the class that contains this function among others.
Code:
void binary32::updateVector(){
int j=0,i = sizeof(long)*8-1;
long mask;
for (; i>=0; i--) {
mask = _intNum & (1<<i);
if(mask == 0)
_intBits[j] = 0;
else
_intBits[j] = 1;
j++;
}
}
and the function of the binary to int is this:
Code:
int binaryToInt(string s){
int i,j=0;
int sum;
for (i=s.size(); i>=0; i--) {
if(s[i] == '1')
sum = sum + pow(2.0,j);
j++;
}
return sum;
}
But I am experiencing some problems. The result of the first function, when entered in the second function, it does not give me the proper result. I cannot find the mistake... Can anyone help?