Reputation: 13
I have an exercise where I am supposed to multiply an unknown number by 5. The number is max 8 bits and stored in 2's compliment. So if the number is 5 the result should be 25 and if its -12 it should be -60.
My only tools are load a register with a bit pattern and add two bit patterns together and store the result. I'm only allowed to use addition 3 times in my program.
With a known number I think I could solve it by adding the appropriate bit pattern to make it 5 times bigger but will that work with an unknown number?
Ex. I have the bit pattern 0001010 representing 10 knowing I want 50 as the result I would add the bit pattern 0101000 to get the resulting 0110010 which is 50.
Can someone please give me a hint how I can solve this problem thank you!
Upvotes: 1
Views: 288
Reputation: 58359
You can compute 5x=2x+2x+x using three additions. Something like this:
y = x + x // y = 2x
z = y + y // z = 2y = 4x
r = z + x // r = 4x + x = 5x
If x
is the unknown value, then at the end of this code, r
is x * 5
.
Upvotes: 4