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 7, 2014 Okay, i get it now. Thanks a lot. Any tips on how to improve my programming skills? Franz Schmidt · December 7, 2014 I made some features in the code, but it got a little messy in the decode function But now every conditions should be fulfilled.``#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 (cmd.find(" ") == strlen("ADD") && cmd.find(",") != strlen("ADD ") && cmd.find(",") != string::npos && cmd.find(",", cmd.find(",") + 1) == string::npos && cmd.find(" ", cmd.find(" ") + 1) != (cmd.find(" ") + 1) && cmd.find(" ", cmd.find(" ") + 1) != cmd.find(" ", cmd.find(" ") + 1)){ 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);}`` Franz Schmidt · December 7, 2014 decode("ADD 23,34"); is NOT a function that you can enter as input.It is the internal function (hope you know how to handle functions in C/C++) which splits the string into the varialbes OPCODE, OP1 and OP2 and this function only is used for this (not for the addition or substraction). Short it just decodes the string into variables so you can handle the instructions easier.What do you don't understand at the second part?And yes this is because atoi() only takes REAL num_chars (from 0 to 9) as valid and the rest is equal to 0Can see that if you execute binary variables (so x = 0) Ed Gon · December 7, 2014 WOW DUDE! I'm totally impressed. I hope reach can your programming skills, I'm skills still sucks but my analysis is keep me hanging on. anyway I'm a bit lost in this part  decode("ADD 23, 34"); Is this an executable command?  as what the instructions says.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.and I found some issue if I input "ADD x,4" the numeric still stores in "y"  and still printed if "PRT AX" is trigger "AX = 4" Franz Schmidt · December 7, 2014 So this is your FINAL INSTRUCTION ``#include #include using namespace std;long long AX = 0;long long OP1 = 0;long long OP2 = 0;enum CMDS {ADD, PRT, EXT, HELP, ERR} OP_CODE;//enum is a variable type which can take different fitsbool EXIT = false;void init(){ OP_CODE = 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 (cmd.find(" ") == strlen("ADD")){ OP_CODE = 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 OP1 = atoi(x.c_str()); OP2 = atoi(y.c_str()); //calculates the result } else{ OP_CODE = ERR; } } else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){//Triggers the PRT AX OP_CODE = PRT; } else if (cmd.find("HELP") == 0 && cmd.length() == strlen("HELP")){//Triggers the HELP OP_CODE = HELP; } else if (cmd.find("EXIT") == 0 && cmd.length() == strlen("EXIT")){//Triggers the EXIT OP_CODE = EXT; } else{ OP_CODE = ERR; }}void execute(){ switch (OP_CODE){ 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: Unknown command!" << endl; break; }}int main(){ string cmd; init(); do{ cmd = getinput(); decode(cmd); execute(); } while (!EXIT);}``Hope you understand everything.If something is not clear just ask Franz Schmidt · December 7, 2014 And the modification isn't that hard you only need to split the main into 3 functions and add those 3 variables. 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; } }}`` 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); 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. 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;``

C++

129,748 followers