Question: Overview You will be manually adding, subtracting, multiplying, and dividing values using bitwise operators. You may not use +, -, *, /, or % in
Overview
You will be manually adding, subtracting, multiplying, and dividing values using bitwise operators. You may not use +, -, *, /, or % in this lab. Instead, you will be writing functions that perform these operations using bitwise operators.
Assignment
Use the following template for your code. You will be writing the functions Multiply, Add, Sub, Div, Mod, and Twos. These functions will use bitwise operators (<<, >>, |, &, ^, ~) for these functions.
Sub must use your two's complement function as well as the add function. Finally, do NOT use flipped_value + 1 in your Two's complement function. Remember, + is not allowed.
You may only use a for loop to run through each bit (0..31), including an iterator, such as i++, and a conditional statement such as i < 32 or i >= 0. The point of allowing this is to preclude you from having to copy and paste your code 32 times (once for each bit).
Do not use any other loops or conditional statements for Add, Twos, or Sub. The point of all of this is to mimic how electrical circuitry will work.
Multiply must use the "shifting" version of multiplication as shown in the lecture notes. Notice that Multiply returns an integer. Generally, you need double the space of the operands, however in this lab, we will not test your code with any numbers that will overflow an integer after multiplication. Make sure you only have the number of loop iterations required for full multiplication. This is not always 32 iterations!
You will need to use loops and conditional statements for division, mod, and multiply.
Implement the restoring division algorithm for both Div and Mod. You must use your bitwise operators to implement this. Implementing any other division algorithm will not yield you any credit. You will notice that these algorithms use add or subtract. Your Add and Sub will not be able to handle these larger data sizes, so for these functions ONLY, you can use + and -.
Copy the template below and write prototyped functions.
#includeint 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
Example Interaction
hydraX: ~> ./lab 5 x 3 15 hydraX: ~> ./lab 5 x -3 -15 hydraX: ~> ./lab 5 - 3 2 hydraX: ~> ./lab 7 / 2 3 hydraX: ~> ./lab 7 % 3 1
Restrictions
- Consider warnings to be as bad as errors. The -Werror below will ensure that all warnings are considered errors.
- You must comment your code, like you did in COSC102, including a header, any TA or student who helped you, and inline-code comments.
- You are not permitted to use any library functions.
- You are not permitted to use arithmetic operators, such as +, - (unary and binary), *, /, or %, except where otherwise noted (such as Div/Mod).
- Do not use conditional (if/else if/else) statements in Add, Sub, or Twos.
- You may use for loops with an iterator and iterator condition (i < 32 or i >=0) as well as arithmetic incrementor/decrementor (i++, i--) only to loop through the 32 bits of a data type. In other words, loops that can be replaced by copying and pasting your code 32 times are permitted.
- You must use the provided template as is. Do not change the template, otherwise the TAs won't be able to accurately test your code.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
