4.9 The sum algorithm for multiplication is not efficient. There are several oth
ID: 664487 • Letter: 4
Question
4.9 The sum algorithm for multiplication is not efficient. There are several other multiplication algorithms available which are much better. One of them is an old one, used already in ancient times, and known as the Russian Peasant Algorithm,. Two multiply AxB. the algorithm can be stated as follows: In this algorithm. A/2 is the integer quotient. discarding the residue or fractional part. a. Test your understanding of the algorithm by multiplying 34 x 27 by hand in decimal terms. b. Multiplying two n-bit unsigned numbers. A and B, the result is a number P with at most 2ti bits. With this in mind, write a pseudocode or draw a flowchart to implement the algorithm and implement it in assembly language for byte operands. c. Repeat the above procedure for 16-bit operands. d. Test your codes in the microcontroller (You may use the simulator).Explanation / Answer
I have written c++ code to do the same. It give you the result of it after every iteration as per algorithm.
#include <iostream>
using namespace std;
unsigned int russianPeasant(unsigned int a, unsigned int b)
{
int res = 0; // initialize result
int i=1;
// While second number doesn't become 1
while (b > 0)
{
if (b & 1)
res = res + a;
a = a << 1;
b = b >> 1;
cout<<"value of a after iteration"<<i<<"is"<<a<<endl;
cout<<"value of b after iteration"<<i<<"is"<<b<<endl;
cout<<"value od result after iteration"<<i<<"is"<<res<<endl;
i++;
}
return res;
}
// Driver program to test above function
int main()
{
cout << russianPeasant(18, 1) << endl;
cout << russianPeasant(20, 12) << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.