# Help me for the insertion of binary function in my add function

 Ed Gon · December 3, 2014 Here is my code, I need the to insert the process where the inputed number converts to binary digit and those numbers will be add in binary form then converts back to decimal as a result. PLEASE HELP~=======#include #include #include using namespace std;int main(){ string cmd; long long ax = 0; bool error = false; cout << "Type in command (type \"HELP\" to see all commands): " <"; getline(cin, cmd);//catches the data if (cmd.find("ADD") == 0){ if (cmd.find(" ") == strlen("ADD")){ string x = cmd.substr(strlen("ADD "), cmd.length()-(cmd.length() - cmd.find(",") + strlen("ADD ")));//store the value of x string y = cmd.substr(cmd.find(",") + 1);//store the value of y ax = atoi(x.c_str()) + atoi(y.c_str()); //calculates the result } else{ error = true; } } else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){//Triggers the PRT AX cout << ">AX = " << ax << endl;//Prints the result } else if (cmd.find("HELP") == 0 && cmd.length() == strlen("HELP")){//Triggers the HELP cout << ">" << endl; cout << ">ADD x,y add x and y and save the result in AX" << endl; cout << ">PRT AX display the value of AX" << endl; cout << ">EXIT exit the application" << endl; cout << ">" << endl; } else if (cmd.find("EXIT") == 0 && cmd.length() == strlen("EXIT")){//Triggers the EXIT break; } else{ error = true; } if (error){ cout << ">Error: Unknown command!" << endl; } }}

## Replies

- page 4

 Franz Schmidt · December 6, 2014 Oh damn forgot about the negative numbers I will have a look at it but it will take me some time because substraction is way more difficult then just adding If you want to try it one your own here is a tutorial how to substract binary nums: https://www.youtube.com/watch?v=S9LJknZTyos Ed Gon · December 6, 2014 ``@franz im stuck up in this format, while merging it in the main prog.//Edwin B. Gonzales Jr.//Csc 112#include #include #include #include #include using namespace std;int main(){ string cmd; long long ax = 0; bool error = false; string x; string y; cout << "Type in command (type \"HELP\" to see all commands): " << endl; while(1){ error = false; cout << ">"; getline(cin, cmd); if (cmd.find("ADD") == 0){ if (cmd.find(" ") == strlen("ADD")){ string x = cmd.substr(strlen("ADD "), cmd.length()-(cmd.length() - cmd.find(",") + strlen("ADD "))); string y = cmd.substr(cmd.find(",") + 1);                    ax = atoi(x.c_str()) + atoi(y.c_str());                    //convert x and y to binary and save binary values in b_x and b_y                        bool b_x[32];                        bool b_y[32];                        unsigned long long x_num = atoi(x.c_str());                        unsigned long long y_num = atoi(y.c_str());                        //converting decimal to binary                        for (int n = 31; n >= 0; n--){                        if ((x_num - pow(2, n)) >= 0){                            b_x[n] = 1;                            x_num -= pow(2, n);                        }                        else{                            b_x[n] = 0;                        }                        if ((y_num - pow(2, n)) >= 0){                            b_y[n] = 1;                            y_num -= pow(2, n);                        }                        else{                            b_y[n] = 0;                        }                    }    cout << endl << "Direct converstion: ";    cout << endl << "b_x: ";    for (int n = 31; n >= 0; n--){ cout << b_x[n];}    cout << endl << "b_y: ";    for (int n = 31; n >= 0; n--){ cout << b_y[n];}    //add numbers in binary form    bool b_sum[32];    bool carry = 0;    for (int n = 0; n < 32; n++){ if (carry){ if (b_x[n] && b_y[n]){ carry = 1; b_sum[n] = 1; } else{ carry = 0; if (b_x[n] || b_y[n]){ carry = 1; b_sum[n] = 0; } else{ b_sum[n] = 1; } } } else{ if (b_x[n] && b_y[n]){ carry = 1; b_sum[n] = 0; } else{ b_sum[n] = b_x[n] | b_y[n]; } }}cout << endl << "sum: ";for (int n = 31; n >= 0; n--){ cout << b_sum[n];}cout << endl;//convert binary sum back to a normal dezimal numberunsigned long long sum = 0;for (int n = 31; n >= 0; n--){ if (b_sum[n] == 1){ sum += pow(2, n); }}    cout << "Sum from binary addition: " << sum  << endl; } else{ error = true; } } else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){ cout << ">AX = " << ax << endl; } else if (cmd.find("HELP") == 0 && cmd.length() == strlen("HELP")){ cout << ">" << endl; cout << ">ADD x,y add x and y and save the result in AX" << endl; cout << ">PRT AX display the value of AX" << endl; cout << ">EXIT exit the application" << endl; cout << ">" << endl; } else if (cmd.find("EXIT") == 0 && cmd.length() == strlen("EXIT")){ break; } else{ error = true; } if (error){ cout << ">Error: Unknown command!" << endl; } } cout << endl << "Sum from normal addition: " << ax;    cout << endl << "Bitset conversion: ";//binary value of ax    bitset < 32 > s(ax);    cout << endl << "sum: " << s << endl;    bitset < 32 > num1(atoi(x.c_str()));    cout << endl << "x: " << num1;//binary value of x    bitset < 32 > num2(atoi(y.c_str()));    cout << endl << "y: " << num2;//binary value of y}``This is the issue I'm handling right now. Ed Gon · December 6, 2014 @franz how did you insert it in the main program? Franz Schmidt · December 5, 2014 I think there is a little problem with the biggest number of unsigned long, but this isn't really a serious problem (won't reach the limit anyway)``cout << endl << "Sum from normal addition: " << ax;cout << endl << "Bitset conversion: ";//binary value of axbitset < 32 > num1(atoi(x.c_str()));cout << endl << "x: " << num1;//binary value of xbitset < 32 > num2(atoi(y.c_str()));cout << endl << "y: " << num2;//binary value of ybitset < 32 > s(ax);cout << endl << "sum: " << s << endl;//convert x and y to binary and save binary values in b_x and b_ybool b_x[32];bool b_y[32];unsigned long long x_num = atoi(x.c_str());unsigned long long y_num = atoi(y.c_str());for (int n = 31; n >= 0; n--){ if ((x_num - pow(2, n)) >= 0){ b_x[n] = 1; x_num -= pow(2, n); } else{ b_x[n] = 0; } if ((y_num - pow(2, n)) >= 0){ b_y[n] = 1; y_num -= pow(2, n); } else{ b_y[n] = 0; }}cout << endl << "Direct converstion: ";cout << endl << "b_x: ";for (int n = 31; n >= 0; n--){ cout << b_x[n];}cout << endl << "b_y: ";for (int n = 31; n >= 0; n--){ cout << b_y[n];}//add numbers in binary formbool b_sum[32];bool carry = 0;for (int n = 0; n < 32; n++){ if (carry){ if (b_x[n] && b_y[n]){ carry = 1; b_sum[n] = 1; } else{ carry = 0; if (b_x[n] || b_y[n]){ carry = 1; b_sum[n] = 0; } else{ b_sum[n] = 1; } } } else{ if (b_x[n] && b_y[n]){ carry = 1; b_sum[n] = 0; } else{ b_sum[n] = b_x[n] | b_y[n]; } }}cout << endl << "sum: ";for (int n = 31; n >= 0; n--){ cout << b_sum[n];}cout << endl;//convert binary sum back to a normal dezimal numberunsigned long long sum = 0;for (int n = 31; n >= 0; n--){ if (b_sum[n] == 1){ sum += pow(2, n); }}cout << "Sum from binary addition: " << sum  << endl;`` Ed Gon · December 5, 2014 @franz it should be on our own without those premade libs. Franz Schmidt · December 5, 2014 Sorry, but I don't understand your code and it won't compile at all But I made a little programm how to add binary values:``bitset s(ax);cout << endl << "Sum normal addition: " << endl << s;//binary value of axbitset num1(atoi(x.c_str()));cout << endl << "x: " << num1;//binary value of xbitset num2(atoi(y.c_str()));cout << endl << "y: " << num2 << endl;//binary value of ybitset sum;bool carry = 0;for (int n = 0; n < 32; n++){ if (carry){ if (num1[n] && num2[n]){ carry = 1; sum[n] = 1; } else{ carry = 0; if (num1[n] || num2[n]){ carry = 1; sum[n] = 0; } else{ sum[n] = 1; } } } else{ if (num1[n] && num2[n]){ carry = 1; sum[n] = 0; } else{ sum[n] = num1[n] | num2[n]; } }}cout << endl << endl << sum << endl << endl;//binary sumcout << endl << sum.to_ullong() << endl << endl;//convert bit to unsigned long long``I estimate that your program is just for excercise purposes. Do you also wan't to convert the number into binary and back directly on your own or with a lib? Ed Gon · December 5, 2014 ``@Franz, I made two seperate codes for the miniADD and Flip but im stuck on how to incorporate it in the main code,miniADD (got some little issues) #include #include using namespace std;int main(){    char a[32];    char b[32];    char carry = '0';    char result[32];    cin >> a >> b;    for(int i = 0; i < 32; i++)    {        if(a == '1' && b == '1') //&& carry == '0')        {            result = '0';            carry = '1';        }        else if(a == '0' && b == '1' && carry == '1')        {           result = '0';           carry = '1';        }        else if(a == '1' && b == '1' && carry == '1')        {            result = '1';            carry = '1';        }        else if(a == '1' && b == '0' && carry == '1')        {            result = '0';            carry = '1';        }        else if(a == '1' && b == '0' && carry == '0')        {            result = '1';            carry = '0';        }        else if(a == '0' && b == '0' && carry == '1')        {            result = '1';            carry = '0';        }        else if(a == '0' && b == '1' && carry == '0')        {            result = '1';            carry = '0';        }        else if(a == '0' && b == '0' && carry == '0')        {            result = '0';            carry = '0';        }    }    result[32] = carry;    for(int j = 32; j >= 0; j--)    {        cout << result[j];    }    cout << endl;    return 0;}``FLIP (not fully working)``#include #include #include #include using namespace std;int main(){    int j;    int x[16];    int i;    string a;    int z = 0;    stringstream convert;    cin >> i;    convert << i;    a = convert.str();    for(j = 0; j < 15; j++)    {        x[j] = a.substr(z, z + 1);        if(x[j] == 0)        {            x[j] = 1;        }        else if(x[j] == 1)        {            x[j] = 0;        }        else        {            cout << " ";        }        z++;    }    cout << x[j];    return 0;}`` Franz Schmidt · December 4, 2014 I give you a little starting point:``bitset b(x);cout << endl << b << endl << b[3];``The b is a binary variable and is an array.There is only the problem with the size of the b variable (CHAR_BIT only is 8) Ed Gon · December 4, 2014 @wheately aw so sorry appreciated the effort! Ed Gon · December 4, 2014 @wheatelyyou just change the interface, its just my code. -.-

## C++

129,871 followers