# Multiplication of two numbers with shift operator

For any given two numbers n and m, you have to find n*m without using any multiplication operator.

Examples:

```Input: n = 25 , m = 13
Output: 325

Input: n = 50 , m = 16
Output: 800
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We can solve this problem with the shift operator. The idea is based on the fact that every number can be represented in binary form. And multiplication with a number is equivalent to multiplication with powers of 2. Powers of 2 can be obtained using left shift operator.

Check for every set bit in the binary representation of m and for every set bit left shift n, count times where count if place value of the set bit of m and add that value to answer.

```// CPP program to find multiplication
// of two number without use of
// multiplication operator
#include<bits/stdc++.h>
using namespace std;

// Function for multiplication
int multiply(int n, int m)
{
int ans = 0, count = 0;
while (m)
{
// check for set bit and left
// shift n, count times
if (m % 2 == 1)
ans += n << count;

// increment of place value (count)
count++;
m /= 2;
}
return ans;
}

// Driver program
int main()
{
int n = 20 , m = 13;
cout << multiply(n, m);
return 0;
}
```

Output:

```260
```

Time Complexity : O(log n)

Related Article:
Russian Peasant (Multiply two numbers using bitwise operators)

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3 Average Difficulty : 3/5.0
Based on 5 vote(s)