Binary Addition,Subtraction,Multiplication and Division

00:02 0 Comments A+ a-

Problem:
Add, Subtract, Multiply and Division of two Binary Numbers 
Solution:
# include <iostream>
# include <cmath>
using namespace std;
int toDec(int binary)
{
int x = 0, a = 0, y = 0;
while (binary != 0)
{
x = binary % 10;
binary = binary / 10;
y = x*pow(2, a) + y;
a++;
}
return y;

}
int powCheck(int x)
{
for (int v = 0; v<1000; v++)
{
if (pow(2, v) == x)
{
return v;
}
}
}
int main()
{
int bin1, bin2, revBin = 0, conBin = 0;
char choice;
cout << "Please Enter 1st Binary Number : ";
cin >> bin1;
cout << "Please Enter 2nd Binary Number : ";
cin >> bin2;
cout << "Please Enter an operation to be done from below" << endl;
cout << "Enter '+'sign to Add Binary Numbers" << endl;
cout << "Enter '-'sign to Subtract Binary Numbers" << endl;
cout << "Enter '*'sign to Multiply Binary Numbers" << endl;
cout << "Enter '/'sign to Divide Binary Numbers" << endl;
cin >> choice;
int d1 = toDec(bin1); //bin1 to decimal
int d2 = toDec(bin2);
cout << "Decimal of 1st Binary is : " << d1 << endl;
cout << "Decimal of 2nd Binary is : " << d2 << endl;

int fun;
if (choice == '+')
{
fun = d1 + d2;
}
else if (choice == '-')
{
fun = d1 - d2;
}
else if (choice == '*')
{
fun = d1*d2;
}
else if (choice == '/')
{
fun = d1 / d2;
}
else
{
return 0;
}
int altFun = fun;
while (fun != 0)
{
revBin = revBin * 10 + fun % 2;
fun = fun / 2;

}
while (revBin != 0)
{
conBin = conBin * 10 + revBin % 10;
revBin = revBin / 10;
}
if (altFun % 2 == 0 && powCheck(altFun))
{
conBin = conBin*pow(10, powCheck(altFun));
cout <<"The result from "<<choice<<" is: "<< conBin << endl;

}
else if (altFun % 2 == 0)
{
do
{
conBin = conBin * 10;
} while (altFun != toDec(conBin));
cout <<"The result from "<<choice<<" is: "<< conBin << endl;
}
else
{
cout <<"The result from "<<choice<<" is: "<< conBin << endl;
}




return 0;


}