Useful Tips For Completing This Practice Numbers Should Be Represented In Binary And Operations Like Shift Right Shif 1 (65.88 KiB) Viewed 9 times
Useful Tips For Completing This Practice Numbers Should Be Represented In Binary And Operations Like Shift Right Shif 2 (27.96 KiB) Viewed 9 times
Useful Tips For Completing This Practice Numbers Should Be Represented In Binary And Operations Like Shift Right Shif 3 (74.08 KiB) Viewed 9 times
Useful tips: For completing this practice, numbers should be represented in binary and operations like shift right, shift left, and testing LSB needs to be performed. This section provides a bit of background how to do these operations. Refer back to this section and figure out what you need from here when implementing the algorithm. To print a number in binary for debugging, use bitset. Example: #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 bie 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 bie int 9 (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 (sa) 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";
// shift right inted: std::cout << std::bitset<32>(e) << " < original number" << std::endl; ee >> 1; // shift to the right std::cout << std::bitset<32>(e) << H < shift right" << std::endl; if ((e & 0x00000001)-1) { std::cout << "1sb of number above is one\n"; } else { } std::cout << "1sb of number above is zero\n"; return 0; Practice 1: Pick one of the multiply or divide algorithm described in the next two pages and complete the practice. Do not complete both.
Option 1- Multiply: Develop multiplication algorithm in C++ and verify the correctness using 5128 as multiplicand and 732 as multiplier. Output should be 3753696. Implement the algorithm exactly as presented in the learning objective. The algorithm is shown below: Multiplier01 1a. Add multiplicand to product and place the result in Product register Product What is the 1st iteration when you get non-zero Product? Start 1. Test Multiplier 2. Shift the Multiplicand register left 1 bit 3. Shift the Multiplier register right 1 bit 32nd repetition? Multiplier = 0 Done Test your code with multiplicand and multipliers in the table below: Multiplicand = 1024 Multiplier = 768 Yes: 32 repetitions No:32 repetitions What to submit? Submit your code (.cpp file) and the completed table. Multiplicand=78772 Multiplier 96 Multiplicand = 98366 Multiplier = 1432
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!