# 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 3

 Ed Gon · December 6, 2014 @franz how did you insert it in the main program? 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. 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 Franz Schmidt · December 6, 2014 So finally made it It was a little bit difficult...``cout << endl << "Direct conversion: ";cout << endl << "b_x: ";for (int n = 30; n >= 0; n--){ cout << b_x[n];}cout << endl << "b_y: ";for (int n = 30; n >= 0; n--){ cout << b_y[n];}//add/substract numbers in binary formbool b_sum[31];bool carry = 0;x_num = atoi(x.c_str());y_num = atoi(y.c_str());if (x_num >= 0 && y_num >= 0 || x_num <= 0 && y_num <= 0){ //addition for (int n = 0; n < 31; 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]; } } }}else{ //substraction bool b_big[31]; bool b_small[31]; bool borrow[31] = { 0 };//variable for substraction if (x_num < 0){ x_num *= (-1); } if (y_num < 0){ y_num *= (-1); } if (x_num > y_num){ memcpy(b_big, b_x, sizeof(b_x)); memcpy(b_small, b_y, sizeof(b_y)); } else{ memcpy(b_big, b_y, sizeof(b_y)); memcpy(b_small, b_x, sizeof(b_x)); } for (int n = 0; n < 31; n++){ if (b_small[n]){ if (!b_big[n] && !borrow[n]){ int u = 1; while (1){ if (b_big[n + u]){ b_big[n + u] = 0; break; } else{ borrow[n + u] = 1; } u++; } b_sum[n] = 1; } else{ if (b_big[n] && borrow[n]){ b_sum[n] = 1; } else{ b_sum[n] = 0; } } } else if (b_big[n]){ if (borrow[n]){ b_sum[n] = 0; } else{ b_sum[n] = 1; } } else{ if (borrow[n]){ b_sum[n] = 1; } else{ b_sum[n] = 0; } } }}cout << endl << "sum: ";for (int n = 30; n >= 0; n--){ cout << b_sum[n];}cout << endl;//convert binary sum back to a normal dezimal numberlong long sum = 0;x_num = atoi(x.c_str());y_num = atoi(y.c_str());for (int n = 30; n >= 0; n--){ if (b_sum[n] == 1){ sum += pow(2, n); }}if ((x_num <= 0 && y_num <= 0) || (x_num < 0 && (x_num * (-1)) > y_num) || (y_num < 0 && (y_num * (-1)) > x_num)){ sum *= (-1);}cout << "Sum from binary addition: " << sum  << endl;`` Franz Schmidt · December 10, 2014 Yes there was a little mistake in the if statement of the syntax error ``#include #include using namespace std;long long AX = 0;long long OP1 = 0;long long OP2 = 0;enum CMDS { ADD, PRT, EXT, HELP, ERR } OPCODE;bool EXIT = false;enum ERRORS { CMD, NUM, SYNTAX} ERR_MSG;void init(){ OPCODE = ERR; cout << "Type in command (type \"HELP\" to see all commands): " << endl;}string getinput(){ string input; cout << ">" << endl << ">"; getline(cin, input);//catches the data return input;}void decode(string cmd){ if (cmd.find("ADD") == 0){ if (/*1*/cmd.find(" ") == strlen("ADD") && /*2*/cmd.find(",") != strlen("ADD ") && /*3*/cmd.find(",") != string::npos && /*4*/cmd.find(",") != (strlen(cmd.c_str()) - 1) && /*5*/cmd.find(",", cmd.find(",") + 1) == string::npos && /*6*/cmd.find(" ", cmd.find(" ") + 1) != (cmd.find(" ") + 1) && /*7*/cmd.find(" ", cmd.find(" ") + 1) == string::npos){ /*fails from the if above: 1. ADD1,2 2. ADD ,2 3. ADD 1, 4. ADD 1 2 5. ADD 1,2, or ADD 1,,2 6. ADD  1,2 7. ADD 1,  2 */ OPCODE = ADD; string x; string y; x = cmd.substr(strlen("ADD "), cmd.length() - (cmd.length() - cmd.find(",") + strlen("ADD ")));//store the value of x if (cmd.find(" ", cmd.find(","), 1) == cmd.find(",") + 1){ y = cmd.substr(cmd.find(" ", cmd.find(",")) + 1);//store the value of y } else{ y = cmd.substr(cmd.find(",") + 1);//store the value of y } if (strspn(x.c_str(), "0123456789") && strspn(y.c_str(), "0123456789")){ OP1 = atoi(x.c_str()); OP2 = atoi(y.c_str()); //calculates the result } else{ OPCODE = ERR; ERR_MSG = NUM; } } else{ OPCODE = ERR; ERR_MSG = SYNTAX; } } else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){//Triggers the PRT AX OPCODE = PRT; } else if (cmd.find("HELP") == 0 && cmd.length() == strlen("HELP")){//Triggers the HELP OPCODE = HELP; } else if (cmd.find("EXIT") == 0 && cmd.length() == strlen("EXIT")){//Triggers the EXIT OPCODE = EXT; } else{ OPCODE = ERR; ERR_MSG = CMD; }}void execute(){ switch (OPCODE){ case ADD: //convert x and y to binary and save binary values in b_x and b_y bool b_x[31]; bool b_y[31]; long long x_num; long long y_num; x_num = OP1; y_num = OP2; for (int n = 30; n >= 0; n--){ if ((x_num - pow(2, n)) >= 0){ b_x[n] = 1; x_num -= pow(2, n); } else 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 if ((y_num + pow(2, n)) <= 0){ b_y[n] = 1; y_num += pow(2, n); } else{ b_y[n] = 0; } } /*cout << endl << "Direct conversion: "; cout << endl << "b_x: "; for (int n = 30; n >= 0; n--){ cout << b_x[n]; } cout << endl << "b_y: "; for (int n = 30; n >= 0; n--){ cout << b_y[n]; }*/ //add/substract numbers in binary form bool b_sum[31]; bool carry; x_num = OP1; y_num = OP2; carry = 0; if (x_num >= 0 && y_num >= 0 || x_num <= 0 && y_num <= 0){ //addition for (int n = 0; n < 31; 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]; } } } } else{ //substraction bool b_big[31]; bool b_small[31]; bool borrow[31] = { 0 };//variable for substraction if (x_num < 0){ x_num *= (-1); } if (y_num < 0){ y_num *= (-1); } if (x_num > y_num){ memcpy(b_big, b_x, sizeof(b_x)); memcpy(b_small, b_y, sizeof(b_y)); } else{ memcpy(b_big, b_y, sizeof(b_y)); memcpy(b_small, b_x, sizeof(b_x)); } for (int n = 0; n < 31; n++){ if (b_small[n]){ if (!b_big[n] && !borrow[n]){ int u = 1; while (1){ if (b_big[n + u]){ b_big[n + u] = 0; break; } else{ borrow[n + u] = 1; } u++; } b_sum[n] = 1; } else{ if (b_big[n] && borrow[n]){ b_sum[n] = 1; } else{ b_sum[n] = 0; } } } else if (b_big[n]){ if (borrow[n]){ b_sum[n] = 0; } else{ b_sum[n] = 1; } } else{ if (borrow[n]){ b_sum[n] = 1; } else{ b_sum[n] = 0; } } } } /*cout << endl << "sum: "; for (int n = 30; n >= 0; n--){ cout << b_sum[n]; } cout << endl;*/ //convert binary sum back to a normal dezimal number long long sum; x_num = OP1; y_num = OP2; sum = 0; for (int n = 30; n >= 0; n--){ if (b_sum[n] == 1){ sum += pow(2, n); } } if ((x_num <= 0 && y_num <= 0) || (x_num < 0 && (x_num * (-1)) > y_num) || (y_num < 0 && (y_num * (-1)) > x_num)){ sum *= (-1); } //cout << "Sum from binary addition: " << sum << endl; AX = sum; break; case PRT: cout << ">AX = " << AX << endl;//Prints the result break; case EXT: EXIT = true; case HELP: 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; break; case ERR: cout << ">Error: "; switch (ERR_MSG){ case CMD: cout << "Unknown command!"; break; case NUM: cout << "Enter only numbers!"; break; case SYNTAX: cout << "Take care of the input syntax!"; break; } cout << endl; break; }}int main(){ string cmd; init(); do{ cmd = getinput(); decode(cmd); execute(); } while (!EXIT);}`` Ed Gon · December 10, 2014 @franz never notice it was on the arguments. i though it was in storing AX, i tried printing it out so that i would know it really do store. anyways thanks for the help again! Ed Gon · December 15, 2014 @franz. btw its buggy sometimes. when you type a wrong command at first it yields different answer, I'm guessing its calculating the command error. anyway thanks again! Ed Gon · December 7, 2014 @franz can you show it, how merge it with the main program? :S because i always got a wrong output. I did this.``#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 conversion: ";                    cout << endl << "b_x: ";                    for (int n = 30; n >= 0; n--){                        cout << b_x[n];                    }                    cout << endl << "b_y: ";                    for (int n = 30; n >= 0; n--){                        cout << b_y[n];                    }                        //add numbers in binary form                       //add/substract numbers in binary form                    bool b_sum[31];                    bool carry = 0;                    x_num = atoi(x.c_str());                    y_num = atoi(y.c_str());                    if (x_num >= 0 && y_num >= 0 || x_num <= 0 && y_num <= 0){                        //addition                        for (int n = 0; n < 31; 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];                                }                            }                        }                    }                    else{                        //substraction                        bool b_big[31];                        bool b_small[31];                        bool borrow[31] = { 0 };//variable for substraction                        if (x_num < 0){                            x_num *= (-1);                        }                        if (y_num < 0){                            y_num *= (-1);                        }                        if (x_num > y_num){                            memcpy(b_big, b_x, sizeof(b_x));                            memcpy(b_small, b_y, sizeof(b_y));                        }                        else{                            memcpy(b_big, b_y, sizeof(b_y));                            memcpy(b_small, b_x, sizeof(b_x));                        }                        for (int n = 0; n < 31; n++){                            if (b_small[n]){                                if (!b_big[n] && !borrow[n]){                                    int u = 1;                                    while (1){                                        if (b_big[n + u]){                                            b_big[n + u] = 0;                                            break;                                        }                                        else{                                            borrow[n + u] = 1;                                        }                                        u++;                                    }                                    b_sum[n] = 1;                                }                                else{                                    if (b_big[n] && borrow[n]){                                        b_sum[n] = 1;                                    }                                    else{                                        b_sum[n] = 0;                                    }                                }                            }                            else if (b_big[n]){                                if (borrow[n]){                                    b_sum[n] = 0;                                }                                else{                                    b_sum[n] = 1;                                }                            }                            else{                                if (borrow[n]){                                    b_sum[n] = 1;                                }                                else{                                    b_sum[n] = 0;                                }                            }                    }                    cout << endl << "sum: ";                    for (int n = 30; n >= 0; n--){                        cout << b_sum[n];                    }                    cout << endl;                    }                    cout << endl << "sum: ";                    for (int n = 31; n >= 0; n--){                        cout << b_sum[n];                    }                    cout << endl;                    //convert binary sum back to a normal dezimal number                    long long sum = 0;                    x_num = atoi(x.c_str());                    y_num = atoi(y.c_str());                    for (int n = 30; n >= 0; n--){                        if (b_sum[n] == 1){                            sum += pow(2, n);                        }                    }                        if ((x_num <= 0 && y_num <= 0) || (x_num < 0 && (x_num * (-1)) > y_num) || (y_num < 0 && (y_num * (-1)) > x_num)){                    sum *= (-1);            }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 will be the results. Ed Gon · December 7, 2014 AND THIS IS THE FINAL INSTRUCTION GIVEN TO US @_@I would like to see that your first exercise has three functions to perform ADD, PRT and EXT operations.   This is to practice modularity in your program.  If you had not done yet, then modify it in this second exercise.Also, have your decoding of the valid instruction be another function. Which means that you can perform this function:     decode("ADD 23, 34");Create the following global variables:OPCODE which will hold the current operation. In the above particular case, it is ADD.OP1 which will hold the first parameter which is 23. This will become optional later when additional operations are included.OP2 which will hold the second parameter, which is 34. This will become optional later when additional operations are included.AX will hold the result for a particular OPCODE. This resembles the one of the registers in the CPU. Later, additional registers will be incuded.EXIT as flag that is turned on when the OPCODE is equal to "EXT"Create also another function execute().  This will just perform the recently decoded instructions.  After the implementation, you can do the function call:    execute(); The first exercise should be modified so that it look like this:  do     input = getinput();     decode(input);     execute();  while (!EXIT); Franz Schmidt · December 7, 2014 This is the whole thing:``#include #include #include #include #include #include #include using namespace std;int main(){ string cmd; unsigned long long ax = 0; bool error = false; cout << "Type in command (type \"HELP\" to see all commands): " << endl; while (1){ error = false; cout << ">"; 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 /*cout << endl << "Sum from normal addition: " << ax; cout << endl << "Bitset conversion: ";//binary value of ax 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 bitset <32> s(ax); cout << endl << "sum: " << s << endl;*/ //convert x and y to binary and save binary values in b_x and b_y bool b_x[31]; bool b_y[31]; long long x_num = atoi(x.c_str()); long long y_num = atoi(y.c_str()); for (int n = 30; n >= 0; n--){ if ((x_num - pow(2, n)) >= 0){ b_x[n] = 1; x_num -= pow(2, n); } else 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 if ((y_num + pow(2, n)) <= 0){ b_y[n] = 1; y_num += pow(2, n); } else{ b_y[n] = 0; } } cout << endl << "Direct conversion: "; cout << endl << "b_x: "; for (int n = 30; n >= 0; n--){ cout << b_x[n]; } cout << endl << "b_y: "; for (int n = 30; n >= 0; n--){ cout << b_y[n]; } //add/substract numbers in binary form bool b_sum[31]; bool carry = 0; x_num = atoi(x.c_str()); y_num = atoi(y.c_str()); if (x_num >= 0 && y_num >= 0 || x_num <= 0 && y_num <= 0){ //addition for (int n = 0; n < 31; 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]; } } } } else{ //substraction bool b_big[31]; bool b_small[31]; bool borrow[31] = { 0 };//variable for substraction if (x_num < 0){ x_num *= (-1); } if (y_num < 0){ y_num *= (-1); } if (x_num > y_num){ memcpy(b_big, b_x, sizeof(b_x)); memcpy(b_small, b_y, sizeof(b_y)); } else{ memcpy(b_big, b_y, sizeof(b_y)); memcpy(b_small, b_x, sizeof(b_x)); } for (int n = 0; n < 31; n++){ if (b_small[n]){ if (!b_big[n] && !borrow[n]){ int u = 1; while (1){ if (b_big[n + u]){ b_big[n + u] = 0; break; } else{ borrow[n + u] = 1; } u++; } b_sum[n] = 1; } else{ if (b_big[n] && borrow[n]){ b_sum[n] = 1; } else{ b_sum[n] = 0; } } } else if (b_big[n]){ if (borrow[n]){ b_sum[n] = 0; } else{ b_sum[n] = 1; } } else{ if (borrow[n]){ b_sum[n] = 1; } else{ b_sum[n] = 0; } } } } cout << endl << "sum: "; for (int n = 30; n >= 0; n--){ cout << b_sum[n]; } cout << endl; //convert binary sum back to a normal dezimal number long long sum = 0; x_num = atoi(x.c_str()); y_num = atoi(y.c_str()); for (int n = 30; n >= 0; n--){ if (b_sum[n] == 1){ sum += pow(2, n); } } if ((x_num <= 0 && y_num <= 0) || (x_num < 0 && (x_num * (-1)) > y_num) || (y_num < 0 && (y_num * (-1)) > x_num)){ sum *= (-1); } cout << "Sum from binary addition: " << sum  << endl; } 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; } }}``

## C++

131,136 followers