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

a = a<<3 - a; or a = a<<2 + a<<1 + a;

Both these statements multiply a by 7.

## C++

`#include<iostream> ` `using` `namespace` `std; ` ` ` `// Returns n * 7 ` `int` `multiplyBySeven(` `int` `n) ` `{ ` ` ` `// OR (n << 2) + (n << 1) + n ` ` ` `return` `(n << 3) - n; ` `} ` ` ` `// Returns n * 12 ` `int` `multiplyByTwelve(` `int` `n) ` `{ ` ` ` `return` `(n << 3) + (n << 2); ` `} ` ` ` `int` `main() ` `{ ` ` ` `cout << multiplyBySeven(5) << endl; ` ` ` `cout << multiplyByTwelve(5) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`class` `GFG { ` ` ` ` ` `// Returns n * 7 ` ` ` `static` `int` `multiplyBySeven(` `int` `n) ` ` ` `{ ` ` ` ` ` `// OR (n << 2) + (n << 1) + n ` ` ` `return` `(n << ` `3` `) - n; ` ` ` `} ` ` ` ` ` `// Returns n * 12 ` ` ` `static` `int` `multiplyByTwelve(` `int` `n) ` ` ` `{ ` ` ` `return` `(n << ` `3` `) + (n << ` `2` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `System.out.println(multiplyBySeven(` `5` `)); ` ` ` `System.out.println(multiplyByTwelve(` `5` `)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to Multiplying a ` `# variable with a constant ` ` ` `# Returns n * 7 ` `def` `multiplyBySeven(n): ` ` ` ` ` `# OR (n << 2) + (n << 1) + n ` ` ` `return` `(n << ` `3` `) ` `-` `n ` ` ` `# Returns n * 12 ` `def` `multiplyByTwelve(n): ` ` ` `return` `(n << ` `3` `) ` `+` `(n << ` `2` `) ` ` ` `# Driver code ` `print` `(multiplyBySeven(` `5` `)) ` `print` `(multiplyByTwelve(` `5` `)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to Multiplying a ` `// variable with a constant ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns n * 7 ` ` ` `static` `int` `multiplyBySeven(` `int` `n) ` ` ` `{ ` ` ` `// OR (n << 2) + (n << 1) + n ` ` ` `return` `(n << 3) - n; ` ` ` `} ` ` ` ` ` `// Returns n * 12 ` ` ` `static` `int` `multiplyByTwelve(` `int` `n) ` ` ` `{ ` ` ` `return` `(n << 3) + (n << 2); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `Console.WriteLine(multiplyBySeven(5)); ` ` ` `Console.WriteLine(multiplyByTwelve(5)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program of multiply operator ` `// Returns n * 7 ` ` ` `function` `multiplyBySeven(` `$n` `) ` `{ ` ` ` `return` `(` `$n` `<< 3) - ` `$n` `; ` `} ` ` ` `// Returns n * 12 ` `function` `multiplyByTwelve(` `$n` `) ` `{ ` ` ` `return` `(` `$n` `<< 3) + (` `$n` `<< 2); ` `} ` ` ` `// Driver Code ` `echo` `multiplyBySeven(5), ` `"\n"` `; ` `echo` `multiplyByTwelve(5), ` `"\n"` `; ` ` ` `// This code is contributed by Ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

35 60

We just need to find the combination of powers of 2. Also, this comes really handy when we have a very large dataset and each one of them requires multiplication with the same constant as bitwise operators are faster as compared to mathematical operators.

This article is contributed by **kp93**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Multiply a number with 10 without using multiplication operator
- Divide two integers without using multiplication, division and mod operator
- Calculate 7n/8 without using division and multiplication operators
- Multiplication of two numbers with shift operator
- How to swap two numbers without using a temporary variable?
- Swap three variables without using temporary variable
- Smallest string without any multiplication sign that represents the product of two given numbers
- Find XOR of two number without using XOR operator
- Compare two integers without using any Comparison operator
- Find largest element from array without using conditional operator
- Maximum OR value of a pair in an Array without using OR operator
- Check a number is odd or even without modulus operator
- Multiplication with a power of 2
- Booth’s Multiplication Algorithm
- Assign other value to a variable from two possible values
- Multiply any Number with 4 using Bitwise Operator
- Find the remainder when N is divided by 4 using Bitwise AND operator
- Add two numbers without using arithmetic operators
- Calculate square of a number without using *, / and pow()
- Subtract two numbers without using arithmetic operators