Related Articles

# Multiplication with a power of 2

• Difficulty Level : Medium
• Last Updated : 07 Apr, 2021

Given two numbers x and n, we need to multiply x with 2n
Examples :

```Input  : x = 25, n = 3
Output : 200
25 multiplied by 2 raised to power 3
is 200.

Input : x = 70, n = 2
Output : 280
```

A simple solution is to compute n-th power of 2 and then multiply with x.

## C++

 `// Simple C/C++ program``// to compute x * (2^n)``#include ``using` `namespace` `std;``typedef` `long` `long` `int` `ll;` `// Returns 2 raised to power n``ll power2(ll n)``{``    ``if` `(n == 0)``        ``return` `1;``        ` `    ``if` `(n == 1)``        ``return` `2;``        ` `    ``return` `power2(n / 2) *``                    ``power2(n / 2);``}` `ll multiply(ll x, ll n)``{``    ``return` `x * power2(n);``}` `// Driven program``int` `main()``{``    ``ll x = 70, n = 2;``    ``cout<

## Java

 `// Simple Java program``// to compute x * (2^n)``import` `java.util.*;` `class` `GFG {``    ` `    ``// Returns 2 raised to power n``    ``static` `long` `power2(``long` `n)``    ``{``        ``if` `(n == ``0``)``            ``return` `1``;``            ` `        ``if` `(n == ``1``)``            ``return` `2``;``            ` `        ``return` `power2(n / ``2``)``                          ``* power2(n / ``2``);``    ``}``     ` `    ``static` `long` `multiply(``long` `x, ``long` `n)``    ``{``        ``return` `x * power2(n);``    ``}``    ` `    ``/* Driver program */``    ``public` `static` `void` `main(String[] args)``    ``{``        ``long` `x = ``70``, n = ``2``;``        ` `        ``System.out.println(multiply(x, n));``    ``}``}``    ` `// This code is contributed by Arnav Kr. Mandal.   `

## Python3

 `# Simple Python program``# to compute x * (2^n)` `# Returns 2 raised to power n``def` `power2(n):` `    ``if` `(n ``=``=` `0``):``        ``return` `1``    ``if` `(n ``=``=` `1``):``        ``return` `2``    ``return` `power2(n ``/` `2``) ``*``                  ``power2(n ``/` `2``);`  `def` `multiply(x, n):``    ``return` `x ``*` `power2(n);`  `# Driven program``x ``=` `70``n ``=` `2``print``(multiply(x, n))` `# This code is contributed by Smitha Dinesh Semwal`

## C#

 `// Simple C# program``// to compute x * (2^n)``using` `System;` `class` `GFG {``    ` `    ``// Returns 2 raised to power n``    ``static` `long` `power2(``long` `n)``    ``{``        ``if` `(n == 0)``            ``return` `1;``            ` `        ``if` `(n == 1)``            ``return` `2;``            ` `        ``return` `power2(n / 2)``                        ``* power2(n / 2);``    ``}``    ` `    ``static` `long` `multiply(``long` `x, ``long` `n)``    ``{``        ``return` `x * power2(n);``    ``}``    ` `    ``/* Driver program */``    ``public` `static` `void` `Main()``    ``{``        ``long` `x = 70, n = 2;``        ` `        ``Console.WriteLine(multiply(x, n));``    ``}``}``    ` `// This code is contributed by Vt_m.`

## PHP

 ``

## Javascript

 ``

Output :

`280`

Time complexity : O(Log n)
An efficient solution is to use bitwise leftshift operator. We know 1 << n means 2 raised to power n.

## C++

 `// Efficient C/C++ program to compute x * (2^n)``#include ``typedef` `long` `long` `int` `ll;` `ll multiply(ll x, ll n)``{``    ``return` `x << n;``}` `// Driven program to check above function``int` `main()``{``    ``ll x = 70, n = 2;``    ``printf``(``"%lld"``, multiply(x, n));``    ``return` `0;``}`

## Java

 `// JAVA Code for Multiplication with a``// power of 2``import` `java.util.*;` `class` `GFG {``    ` `    ``static` `long` `multiply(``long` `x, ``long` `n)``    ``{``        ``return` `x << n;``    ``}``    ` `    ``/* Driver program to test above function */``    ``public` `static` `void` `main(String[] args)``    ``{``        ``long` `x = ``70``, n = ``2``;``        ``System.out.println(multiply(x, n));``    ``}``}` `//This code is contributed by Arnav Kr. Mandal.`

## Python3

 `# Efficient Python3 code to compute x * (2^n)` `def` `multiply( x , n ):``    ``return` `x << n``    ` `# Driven code to check above function``x ``=` `70``n ``=` `2``print``( multiply(x, n))` `# This code is contributed by "Sharad_Bhardwaj".`

## C#

 `// C# Code for Multiplication with a``// power of 2``using` `System;` `class` `GFG {``    ` `    ``static` `int` `multiply(``int` `x, ``int` `n)``    ``{``        ``return` `x << n;``    ``}``    ` `    ``/* Driver program to test above function */``    ``public` `static` `void` `Main()``    ``{``        ``int` `x = 70, n = 2;``    ` `        ``Console.WriteLine(multiply(x, n));``    ``}``}` `//This code is contributed by vt_m.`

## PHP

 ``

## Javascript

 ``

Output :

`280`

Time complexity : O(1)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up