Home /
Expert Answers /
Computer Science /
develop-multiplication-algorithm-in-c-and-verify-the-correctness-using-5128-as-multiplicand-and-73-pa906
(Solved): Develop multiplication algorithm in C++ and verify the
correctness using 5128 as multiplicand and 73 ...
Develop multiplication algorithm in C++ and verify the
correctness using 5128 as multiplicand and 732 as multiplier.
Output should be 3753696.
The algorithm is shown below:
To
print a number in binary for debugging, use bitset:
Example:
Multiplier0 = 1 1a. Add multiplicand to product and place the result in Product register Start 1. Test Multiplier0 2. Shift the Multiplicand register left 1 bit Multiplier0 = 0 3. Shift the Multiplier register right 1 bit 32nd repetition? Done No: < 32 repetitions Yes: 32 repetitions
Test your code with multiplicand and multipliers in the table below: Multiplicand = 1024 Multiplier = 768 Product What is the 1st iteration when you get non-zero Product? Multiplicand = 78772 Multiplier = 96 Multiplicand = 98366 Multiplier = 1432
#include #include int main() { } int a = 54; std::cout << std::bitset<32>(a) << std::endl; return 0; In C++, int numbers are 32-bit and long long int numbers are 64 bit. Some more binary operations you can perform on these data types are listed below: #include #include int main() { // original int a = 54; std::cout << std::bitset<64>(a) << "< original" << std::endl; // shift left and insert 1 in least significant bit int b = (a << 1) | 0x00000001; std::cout << std::bitset<64>(b) << "< shift-left insert 1" << std::endl; // shift left and insert 0 in least significant bit int g = (a << 1) & 0xfffffffe; std::cout << std::bitset<64>(g) << "< shift-left insert 0" << std::endl; // load into upper 64-bit long long int c = a; c = c << 32; std::cout << std::bitset<64>(c) << " < insert into upper 32-bit" << std::endl; // test least-significant bit (LSB) int d = 5; std::cout << std::bitset<32>(d) << " < original value" << std::endl; if ((d & 0x00000001) == 1) { std::cout << "1sb of number above is one\n"; else { } std::cout << "1sb of number above is zero\n";