# Russian Peasant (Multiply two numbers using bitwise operators)

Given two integers, write a function to multiply them without using multiplication operator.

There are many other ways to multiply two numbers (For example, see this). One interesting method is the Russian peasant algorithm. The idea is to double the first number and halve the second number repeatedly till the second number doesn’t become 1. In the process, whenever the second number become odd, we add the first number to result (result is initialized as 0)

The following is simple algorithm.

Let the two given numbers be 'a' and 'b' 1) Initialize result 'res' as 0. 2) Do following while 'b' is greater than 0 a) If 'b' is odd, add 'a' to 'res' b) Double 'a' and halve 'b' 3) Return 'res'.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

## C++

`#include <iostream>` `using` `namespace` `std;` `// A method to multiply two numbers using Russian Peasant method` `unsigned ` `int` `russianPeasant(unsigned ` `int` `a, unsigned ` `int` `b)` `{` ` ` `int` `res = 0; ` `// initialize result` ` ` `// While second number doesn't become 1` ` ` `while` `(b > 0)` ` ` `{` ` ` `// If second number becomes odd, add the first number to result` ` ` `if` `(b & 1)` ` ` `res = res + a;` ` ` `// Double the first number and halve the second number` ` ` `a = a << 1;` ` ` `b = b >> 1;` ` ` `}` ` ` `return` `res;` `}` `// Driver program to test above function` `int` `main()` `{` ` ` `cout << russianPeasant(18, 1) << endl;` ` ` `cout << russianPeasant(20, 12) << endl;` ` ` `return` `0;` `}` |

## Java

`// Java program for Russian Peasant Multiplication` `import` `java.io.*;` `class` `GFG` `{` ` ` `// Function to multiply two` ` ` `// numbers using Russian Peasant method` ` ` `static` `int` `russianPeasant(` `int` `a, ` `int` `b)` ` ` `{` ` ` `// initialize result` ` ` `int` `res = ` `0` `; ` ` ` ` ` `// While second number doesn't become 1` ` ` `while` `(b > ` `0` `)` ` ` `{` ` ` `// If second number becomes odd,` ` ` `// add the first number to result` ` ` `if` `((b & ` `1` `) != ` `0` `)` ` ` `res = res + a;` ` ` ` ` `// Double the first number` ` ` `// and halve the second number` ` ` `a = a << ` `1` `;` ` ` `b = b >> ` `1` `;` ` ` `}` ` ` `return` `res;` ` ` `}` ` ` ` ` `// driver program` ` ` `public` `static` `void` `main (String[] args)` ` ` `{` ` ` `System.out.println(russianPeasant(` `18` `, ` `1` `));` ` ` `System.out.println(russianPeasant(` `20` `, ` `12` `));` ` ` `}` `}` `// Contributed by Pramod Kumar` |

## Python 3

`# A method to multiply two numbers` `# using Russian Peasant method` `# Function to multiply two numbers` `# using Russian Peasant method` `def` `russianPeasant(a, b):` ` ` `res ` `=` `0` `# initialize result` ` ` `# While second number doesn't` ` ` `# become 1` ` ` `while` `(b > ` `0` `):` ` ` ` ` `# If second number becomes` ` ` `# odd, add the first number` ` ` `# to result` ` ` `if` `(b & ` `1` `):` ` ` `res ` `=` `res ` `+` `a` ` ` `# Double the first number` ` ` `# and halve the second` ` ` `# number` ` ` `a ` `=` `a << ` `1` ` ` `b ` `=` `b >> ` `1` ` ` ` ` `return` `res` `# Driver program to test` `# above function` `print` `(russianPeasant(` `18` `, ` `1` `))` `print` `(russianPeasant(` `20` `, ` `12` `))` `# This code is contributed by` `# Smitha Dinesh Semwal` |

## C#

`// C# program for Russian Peasant Multiplication` `using` `System;` `class` `GFG {` ` ` ` ` `// Function to multiply two` ` ` `// numbers using Russian Peasant method` ` ` `static` `int` `russianPeasant(` `int` `a, ` `int` `b)` ` ` `{` ` ` `// initialize result` ` ` `int` `res = 0;` ` ` `// While second number doesn't become 1` ` ` `while` `(b > 0) {` ` ` ` ` `// If second number becomes odd,` ` ` `// add the first number to result` ` ` `if` `((b & 1) != 0)` ` ` `res = res + a;` ` ` `// Double the first number` ` ` `// and halve the second number` ` ` `a = a << 1;` ` ` `b = b >> 1;` ` ` `}` ` ` `return` `res;` ` ` `}` ` ` `// driver program` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `Console.WriteLine(russianPeasant(18, 1));` ` ` `Console.WriteLine(russianPeasant(20, 12));` ` ` `}` `}` `// This code is contributed by Sam007.` |

## PHP

`<?php` `// PHP Code to multiply two numbers` `// using Russian Peasant method` `// function returns the result` `function` `russianPeasant(` `$a` `, ` `$b` `)` `{` ` ` ` ` `// initialize result` ` ` `$res` `= 0;` ` ` `// While second number` ` ` `// doesn't become 1` ` ` `while` `(` `$b` `> 0)` ` ` `{` ` ` ` ` `// If second number becomes odd,` ` ` `// add the first number to result` ` ` `if` `(` `$b` `& 1)` ` ` `$res` `= ` `$res` `+ ` `$a` `;` ` ` `// Double the first number and` ` ` `// halve the second number` ` ` `$a` `= ` `$a` `<< 1;` ` ` `$b` `= ` `$b` `>> 1;` ` ` `}` ` ` `return` `$res` `;` `}` ` ` `// Driver Code` ` ` `echo` `russianPeasant(18, 1), ` `"\n"` `;` ` ` `echo` `russianPeasant(20, 12), ` `"\n"` `;` ` ` `// This code is contributed by Ajit` `?>` |

## Javascript

`<script>` `// A method to multiply two numbers using Russian Peasant method` `function` `russianPeasant(a, b)` `{` ` ` `var` `res = 0; ` `// initialize result` ` ` `// While second number doesn't become 1` ` ` `while` `(b > 0)` ` ` `{` ` ` ` ` `// If second number becomes odd, add the first number to result` ` ` `if` `(b & 1)` ` ` `res = res + a;` ` ` `// Double the first number and halve the second number` ` ` `a = a << 1;` ` ` `b = b >> 1;` ` ` `}` ` ` `return` `res;` `}` `// Driver program to test above function` `document.write(russianPeasant(18, 1)+` `"<br>"` `);` `document.write(russianPeasant(20, 12));` ` ` ` ` `// This code is contributed by noob2000.` `</script>` |

Output:

18 240

**Time Complexity: **O(log_{2}b)

**Auxiliary Space: **O(1)

**How does this work?**

The value of a*b is same as (a*2)*(b/2) if b is even, otherwise the value is same as ((a*2)*(b/2) + a). In the while loop, we keep multiplying ‘a’ with 2 and keep dividing ‘b’ by 2. If ‘b’ becomes odd in loop, we add ‘a’ to ‘res’. When value of ‘b’ becomes 1, the value of ‘res’ + ‘a’, gives us the result.

Note that when ‘b’ is a power of 2, the ‘res’ would remain 0 and ‘a’ would have the multiplication. See the reference for more information.**Reference:**

http://mathforum.org/dr.math/faq/faq.peasant.html

This article is compiled by **Shalki Agarwal**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above