Question: I have this code, which needs to be corrected. NOTICE: You can NOT change int main() , only functions below it. The code should also

I have this code, which needs to be corrected. NOTICE: You can NOT change int main() , only functions below it. The code should also ONLY use bitwise operators. Thank you, I will be sure to leave a like!

#include  int Multiply(int leftop, int rightop); int Add(int leftop, int rightop); int Sub(int leftop, int rightop); int Twos(int op); int Div(int leftop, int rightop); int Mod(int leftop, int rightop);
int main(int argc, char *argv[]) { int left, right, result; char op; if (4 != argc) { printf("Usage: %s    ", argv[0]); return -1; } sscanf(argv[1], "%d", &left); sscanf(argv[2], "%c", &op); sscanf(argv[3], "%d", &right); switch (op) { case 'm': case 'x': case '*': result = Multiply(left, right); break; case 'a': case '+': result = Add(left, right); break; case 's': case '-': result = Sub(left, right); break; case 'd': case '/': result = Div(left, right); break; case '%': result = Mod(left, right); break; default: result = -11111111; break; } printf("%d ", result); return 0; }
//Write your functions here

//End of Prompt

//Function for Addition char Add(char leftop, char rightop){

//Loop to get through all Bits // for (int i; i < 32; i++) { while(rightop){

//Finding shared 1s for both inputs int sum = leftop & rightop;

//XOR sets double true to false and stores in leftop leftop = leftop ^ rightop;

//Shift rightop over to repeat loop rightop = sum << 1; } //Return stored sum return leftop; }

//Function for Two's compliment char Twos(char op){

int flip = 0;

int sto = op < 0 ? 1 : -1; while (op != 0){ flip += sto; op += sto; } return flip; }

//Function for Two's compliment char Twos(char op){

int flip = 0;

int sto = op < 0 ? 1 : -1; while (op != 0){ flip += sto; op += sto; } return flip; }

// Subtract Function

char Sub(char leftop, char rightop) { return leftop + Twos(rightop); }

// Multiply Function short Multiply(char leftop, char rightop) { if (leftop < rightop) return Multiply(rightop, leftop);

int sum = 0; for (int i = abs(rightop); i > 0; i--) sum += leftop;

if (rightop < 0) sum = Twos(sum);

return sum; }

// Division Function char Div(char leftop, char rightop){ int quotient = 0, dividend;

int divisor = Twos(abs(rightop));

for (dividend = abs(leftop); dividend >= abs(divisor); dividend += divisor) quotient++;

if ((leftop<0 && rightop> 0) || (leftop > 0 && rightop < 0)) quotient = Twos(quotient); return quotient; }

//End of code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

This question involves correcting a series of C functions for arithmetic operations using only bitwi... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!