@ ft95
Change line 22 from this:
to this:
That is, subtract the ASCII value of digit '0' from the value which was input.
The calculation of the result can be simplified. Although this works,
29 30
|
for(power=0,counter=i;power<=i;power++,counter--)
sum=pow(2,power)*binarrey[counter]+sum;
|
it would be more straightforward like this:
29 30
|
for (counter=i; counter >= 0; counter--)
sum = sum*2 + binarrey[counter];
|
One more comment, the variable
sum
does not need to be of type
double
. It will only ever contain an
integer
value.
----------------------------------------------------------------------------
I add the following as background information, if it doesn't quite make sense now, then don't worry too much.
If sum is an integer type, you could do this:
29 30 31 32 33
|
for (counter=i; counter >= 0; counter--)
{
sum <<= 1;
sum |= binarrey[counter];
}
|
Although that looks different, it gives the same (correct) result. What this code does, in effect, is to take the binary digits which were input, and place each one into the sum. The 'or' operator
|=
puts the current binary digit (bit) into the right-most position. The left shift operator
<<=
then slides the whole value one position to the left, to make way for the next digit.
This is a fairly direct way of taking a binary input and forming the corresponding binary integer.