Array ( [0] => [1] => questions [2] => Basic [3] => Multiplication-of-Two-numbers )
Given two integers. You have to perform multiplication operation between two numbers using bitwise operators.
First line contains two integers separated by space.
Print the resultant value.
1 3
3
5 7
35
10 12
120
Login to see Discussion
Approach 1:
Step-i)get the minimum of both the numbers
Step-ii) assign minimum number to num1 and other number to num2
Step-iii)initialize a variable to 0
Step-iv) if and operation between num2 and 1 is 1 add num1 to the variable
Step-v)right shift bits of num1 by one position and left shift bits of num2 by one position
Step-vi) repeat steps iv and v until num2 is greater than 0
Step-vii) return answer
Time Complexity: O(logn) where n is maximum of two numbers
Space Complexity: O(1)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution