Multiplication with a power of 2
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 <bits/stdc++.h> 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<<multiply(x, n); return 0; } |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 |
chevron_right
filter_none
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. |
chevron_right
filter_none
PHP
<?php // Simple PHP program // to compute x * (2^n) // Returns 2 raised to power n function 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 ?> |
chevron_right
filter_none
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 <stdio.h> 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; } |
chevron_right
filter_none
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. |
chevron_right
filter_none
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". |
chevron_right
filter_none
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. |
chevron_right
filter_none
PHP
<?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 ?> |
chevron_right
filter_none
Output :
280
Time complexity : O(1)
Recommended Posts:
- Check if given number is a power of d where d is a power of 2
- Booth’s Multiplication Algorithm
- Multiply a number with 10 without using multiplication operator
- Calculate 7n/8 without using division and multiplication operators
- Multiplication of two numbers with shift operator
- Divide two integers without using multiplication, division and mod operator
- Multiplying a variable with a constant without using multiplication operator
- Fibonacci Power
- Number of pairs whose sum is a power of 2
- Check if a number is power of 8 or not
- Program to find whether a no is power of two
- Find whether a given number is a power of 4 or not
- Quotient and remainder dividing by 2^k (a power of 2)
- Minimum absolute difference between N and a power of 2
- Highest power of 2 less than or equal to given number
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t