Multiplication with a power of 2 - GeeksforGeeks

07 Apr, 2021
Given two numbers x and n, we need to multiply x with 2nExamples :  
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++
Java
Python3
C#
PHP
Javascript
// Simple C/C++ program// to compute x * (2^n)#include <bits/stdc++.h>using namespace std;typedef long long int ll; // Returns 2 raised to power nll 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 programint main(){    ll x = 70, n = 2;    cout<<multiply(x, n);    return 0;}
// 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.   
# Simple Python program# to compute x * (2^n) # Returns 2 raised to power ndef 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 programx = 70n = 2print(multiply(x, n)) # This code is contributed by Smitha Dinesh Semwal
// 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// Simple PHP program// to compute x * (2^n) // Returns 2 raised to power nfunction power2($n){    if ($n == 0)        return 1;             if ($n == 1)        return 2;             return power2($n / 2) *           power2($n / 2);} function multiply( $x, $n){    return $x * power2($n);} // Driver Code$x = 70; $n = 2;echo multiply($x, $n); // This code is contributed by ajit?>
<script> // Simple JavaScript program// to compute x * (2^n) // Returns 2 raised to power nfunction power2(n){    if (n == 0)        return 1;             if (n == 1)        return 2;             return power2(n / 2) *        power2(n / 2);} function multiply( x, n){    return x * power2(n);} // Driver Codelet x = 70let n = 2;document.write( multiply(x, n)); // This code is contributed by mohan </script>
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++
Java
Python3
C#
PHP
Javascript
// Efficient C/C++ program to compute x * (2^n)#include <stdio.h>typedef long long int ll; ll multiply(ll x, ll n){    return x << n;} // Driven program to check above functionint main(){    ll x = 70, n = 2;    printf("%lld", multiply(x, n));    return 0;}
// JAVA Code for Multiplication with a// power of 2import 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.
# Efficient Python3 code to compute x * (2^n) def multiply( x , n ):    return x << n     # Driven code to check above functionx = 70n = 2print( multiply(x, n)) # This code is contributed by "Sharad_Bhardwaj".
// C# Code for Multiplication with a// power of 2using 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// Efficient PHP program to compute x * (2^n) function multiply($x, $n){    return $x << $n;}     // Driver Code    $x = 70;    $n = 2;    echo multiply($x, $n);     // This code is contributed by ajit?>
<script> // Efficient JavaScript program to compute x * (2^n) function multiply(x, n){    return x << n;}     // Driver Code    let x = 70;    let n = 2;    document.write(multiply(x, n)); // This code is contributed by mohan </script>
Output : 
280
Time complexity : O(1)  
YouTube<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=O4lIpYTLlp0" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
jit_t
pulamolusaimohan
icon resources
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Set, Clear and Toggle a given bit of a number in C
Check whether K-th bit is set or not
Write an Efficient Method to Check if a Number is Multiple of 3
Reverse actual bits of the given number
Program to find parity
Swap bits in a given number
Generate n-bit Gray Codes
Check if a Number is Odd or Even using Bitwise Operators
Count total bits in a number
Check for Integer Overflow