Multiplying a variable with a constant without using multiplication operator - GeeksforGeeks
As we know that every number can be represented as sum(or difference) of powers of 2, therefore what we can do is represent the constant as a sum of powers of 2.
For this purpose we can use the bitwise left shift operator. When a number is bitwise left shifted it is multiplied by 2 for every bit shift.
For example, suppose we want to multiply a variable say "a" by 10 then what we can do is
a = a << 3 + a << 1;
The expression a << 3 multiplies a by 8 ans expression a<<1 multiplies it by 2.
So basically what we have here is a = a*8 + a*2 = a*10
Similarly for multiplying with 7 what we can do is
Read full article from Multiplying a variable with a constant without using multiplication operator - GeeksforGeeks
No comments:
Post a Comment