Multiply any Number with 4 using Bitwise Operator

09 Mar, 2022
We are a Number n and our task is multiply the number with 4 using bit-wise Operator.Examples:  
Input : 4
Output :16

Input :5
Output :20
 
Explanation Case 1:- n=4 the binary of 4 is 100 and now shift two bit right then 10000 now the number is 16 that is multiply 4*4=16 ans. Approach :- (n<<2) shift two bit right 
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to multiply a number with// 4 using Bitwise Operator#include <bits/stdc++.h>using namespace std; // function the return multiply a number//  with 4 using bitwise operatorint multiplyWith4(int n){    // returning a number with multiply    // with 4 using2 bit shifting right    return (n << 2);} // derive functionint main(){    int n = 4;    cout << multiplyWith4(n) << endl;    return 0;}
// Java program to multiply a number// with 4 using Bitwise Operator class GFG {     // function the return// multiply a number// with 4 using bitwise// operatorstatic int multiplyWith4(int n){         // returning a number    // with multiply with    // 4 using 2 bit shifting    // right    return (n << 2);} // Driver Codepublic static void main(String[] args){    int n = 4;    System.out.print(multiplyWith4(n));}} // This code is contributed by Smitha.
# Python 3 program to multiply# a number with 4 using Bitwise# Operator # function the return multiply# a number with 4 using bitwise# operatordef multiplyWith4(n):     # returning a number with    # multiply with 4 using2    # bit shifting right    return (n << 2) # derive functionn = 4print(multiplyWith4(n)) # This code is contributed# by Smitha
// C# program to multiply a number// with 4 using Bitwise Operatorusing System; class GFG {     // function the return// multiply a number// with 4 using bitwise// operatorstatic int multiplyWith4(int n){         // returning a number    // with multiply with    // 4 using 2 bit shifting    // right    return (n << 2);} // Driver Codepublic static void Main(String[] args){    int n = 4;    Console.Write(multiplyWith4(n));}} // This code is contributed by Smitha.
<?php// PHP program to multiply// a number with 4 using// Bitwise Operator // function the return// multiply a number// with 4 using bitwise// operatorfunction multiplyWith4($n){         // returning a number    // with multiply with    // 4 using2 bit    // shifting right    return ($n << 2);}     // Driver Code    $n = 4;    echo multiplyWith4($n),"\n"; // This code is contributed by Ajit.?>
<script>// javascript program to multiply a number// with 4 using Bitwise Operator  // function the return// multiply a number// with 4 using bitwise// operatorfunction multiplyWith4(n){         // returning a number    // with multiply with    // 4 using 2 bit shifting    // right    return (n << 2);} // Driver Codevar n = 4;document.write(multiplyWith4(n)); // This code is contributed by Amit Katiyar</script>
Output :-  
16
Generalization : In general, we can multiply with a power of 2 using bitwise operators. For example, suppose we wish to multiply with 16 (which is 24), we can do it by left shifting by 4. 
Smitha Dinesh Semwal
jit_t
amit143katiyar
simmytarika5
sumitgumber28
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Builtin functions of GCC compiler
Calculate XOR from 1 to n.
Google Online Challenge 2020
Find two numbers from their sum and XOR
Highest power of 2 less than or equal to given number
Calculate square of a number without using *, / and pow()
Reverse actual bits of the given number
Find XOR of two number without using XOR operator
Count number of bits to be flipped to convert A to B