• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

I've got to write this program for C++

What it says is:
For this lab, we will do multiplication of unsigned integers using integer additions, bitwise shifts, and bit testing.

1. If the multiplier bit is 1, put a copy of the multiplicand in the proper plce and add it to the product or
2. If the multiplier bit is 0, do nothing (add 0 to the product).

for (int i=1; i<=32; i++)

{
If the least significant bit of the multiplier is 1
add multiplicand to the product;
else do nothing;

shift the multiplicand left by 1 bit; //i.e. put it in proper place
shift the multiplier right 1 bit; //so we just need to test the least significant bit
}

your task in this lab is to implement the above algorithm in C++

the compiler reads regular decimal bits in binary i guess but how do i check to see if the least significant bit is 1 or 0?
 
<< = shift left (x << y = x * 2^y)
>> = shift right (x >> y = x / 2^y)
Actually shift right is all you need, I think shift left is slower than addition on some CPUs and you never need to shift more than 1 bit at a time.

The algorithm you are looking for will be in any book on 6502 or similar era processor assembly -- those old CPUs didn't have multiplication and division operations built in so a subroutine to get that job done is pretty much a textbook problem.

Also, rather than always looping 32 times, the condition to break out of the loop can be that the number getting shifted right becomes 0. (This will make the loop a lot faster in what should be the more common cases).
 
Back
Top