Multiply given number by 7 without using multiplication operator.
Obvious answer: Add number to itself 7 times!
But, then think - why the Que asks you multiplying specifically by 7. The above trick of adding numbers is generic and can be applied for any multiplier other than 7. The trick here is as follows. If the given number is 'x', x*7 = x*(8-1) = x*8 - x. At this point, you might note that this involved multiplication again. But here is the main step - Multiplication by any number which is a power of 2, can be done easily by shifting the bits in the binary representation of the number. For binary representation, see this. And this gives the answer - Shift x's bits 3 positions to left and subtract x from it!
No comments:
Post a Comment