Skip to content

Instantly share code, notes, and snippets.

@RezaBidar
Created January 11, 2016 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RezaBidar/de4dff64bbb3c35c91c2 to your computer and use it in GitHub Desktop.
Save RezaBidar/de4dff64bbb3c35c91c2 to your computer and use it in GitHub Desktop.
add 2 binary value
#include<iostream>
#include<cmath>
using namespace std;
int decimal_binary(int n) /* Function to convert decimal to binary.*/
{
int rem, i = 1, binary = 0;
while (n != 0)
{
rem = n % 2;
n /= 2;
binary += rem*i;
i *= 10;
}
return binary;
}
int binary_decimal(int n) /* Function to convert binary to decimal.*/
{
int decimal = 0, i = 0, rem;
while (n != 0)
{
rem = n % 10;
n /= 10;
decimal += rem*pow(2, i);
++i;
}
return decimal;
}
int main()
{
int a, b;
cin >> a >> b;
cout << decimal_binary( binary_decimal(a) + binary_decimal(b));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment