Converting Binary to decimal

I've been on this homework problem for about 2 hours and I just can't wrap my mind around it. It is driving me frickin' crazy.....

So here's the question: Write a function that takes an integer value n whose all of its digits are zeros and/or ones and returns the corresponding decimal value, test the function. (use the function intPow ( ) developed in Question 4 above).


Here's my failed 2-hour effort:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int b2d(int a);
void main()
{
	int a;
	cout<<"Enter a binary number and its number of digits to find the decimal: ";
	cin>>a;
	cout<<"In decimal, "<<a<<"="<<b2d(a)<<endl;

}
int b2d(int a){
	int tot=0;
	int two=1;
	for(int i=0;i<3;i++){
		two=two*2;
		for(a;a>0;a=a/10){
			a=a%10;
		}
		tot=tot+(a*two);
	}
	return tot;
}


This is the code he was referring to from Question4 (which I solved):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;
int intPow(int x,int n);
void main()
{
	int x,n;
	cout<<"Enter two integers x,n, to compute x^n: ";
	cin>>x>>n;
	cout<<x<<"^"<<n<<"= "<<intPow(x,n)<<endl;
}
int intPow(int x,int n){
	int tot=1;
	for(int i=0;i<n;i++){
		tot=tot*x;
	}
	return tot;
}


Basically, In my answer, I tried using methods from two programs to make the converter work. I used a loop to isolate each number of the binary digit alone, like 1101 would be handled as 1, 1, 0 and 1. and the other for doing the calculation of converting.
Basically what I'm failing at is doing this:
decimal =1*2^0 + 0*2^1 + 1*2^2 + 1*2^3 = 13

The isolation idea i'm trying to use is from code i used two days ago:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;
int digitSum(int a);
void main()
{
	int a;
	cout<<"Enter a integer: ";
	cin>>a;
	cout<<"The sum of "<<a<<"'s digits is: "<<digitSum(a)<<endl;
}
int digitSum(int a){
	int tot=0;
	for(a;a>0;a=a/10){
		tot=tot+a%10;
	}
	return tot;
}



Guys I simply can't find a solution to this. Please help me and aim me in the right direction..........
I have the same problem and i cant reach any solution too, i think question 4 you are indicating to is same as the question that i have and i used this code instead


#include <iostream>
using namespace std;
int intPow (int x,int n) {
int i=1, power;
for(power=x;i<n;i++) {
power=power*x;
}
return power;
}
int main () {
int x,n;
cout<<" enter your prefered interger with an exponent:"<<endl;
cout<<"your integer=";
cin>>x;
cout<<"power=";
cin>>n;
cout<<"the outcome is=";
cout<<intPow(x,n)<<endl;
return 0;
}

i ddnt reach to a solution yet, if anyone knows how to solve please help to the right way.
The inner loop in b2d() is consuming 'a' completely the first time through the outer loop, so there's nothing left for the other times. You should only use one loop and its condition should be a>0.
so this means that this part of the firt loop should be excluded in order to use one loop for (for):


for(int i=0;i<3;i++){
two=two*2;

and to use this part

for(a;a>0;a=a/10){
a=a%10;


to satisfy that a>0

it doesn't work :S
Last edited on
It is complete!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int binaryToBase10(int n) {

    int output = 0;

    for(int i=0; n > 0; i++) {

        if(n % 10 == 1) {
            output += pow(2, i);
        }
        n /= 10;
    }

    return output;
}


You're very welcome!
But I would prob not use the pow() function, which is intended for doubles. Use operator<< instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int binaryToBase10(int n)
{
    int output = 0;

    for(int i=0; n > 0; i++) {

        if(n % 10 == 1) {
            output += (1 << i);
        }
        n /= 10;
    }

    return output;
}


Or maybe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int binaryToBase10(int n)
{
    int output = 0;

    int m = 1;

    for(int i=0; n > 0; i++) {

        if(n % 10 == 1) {
            output += m;
        }
        n /= 10;
        m <<= 1;
    }

    return output;
}
Last edited on
Andy, that is an excellent example of using a bitwise shifting operator.

It does the same thing as my code, except it is a LOT more efficient. Usually you don't have to worry about optimizing your code that much because the compile will do it for you. For example, if in your code you multiply a variable by 2
n_my_int *= 2;
You're compiler will automatically change it to:
n_my_int <<= 1

You can read about bitwise operators on this site, or here: http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/
Funnily enough, one time I wrote x<<=1; and VC++ replaced it with x+=x;.
@helios Was this in the olden days? Or recently? If the latter, do you recall how you were using <<+ <<= ?

[edited to correct type helios pointed out]
Last edited on
It was in 2008.

<<+
???
Is that a typo for <<=? If it is, then:
I was trying to write, for fun, a bitwise adder. It was both funny and frustrating that the optimizer decided to make my efforts pointless by compiling x<<=1 into add eax, eax.
Topic archived. No new replies allowed.