Multiply any Number with 4 using Bitwise Operator Last Updated : 25 Nov, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report We are a Number n and our task is to multiply the number by 4 using a 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 shifts two bit right then 10000 now the number is 16 is multiplied 4*4=16 ans. Approach :- (n<<2) shift two bit right C++ // 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 operator int multiplyWith4(int n) { // returning a number with multiply // with 4 using2 bit shifting right return (n << 2); } // derive function int main() { int n = 4; cout << multiplyWith4(n) << endl; return 0; } Java // Java program to multiply a number // with 4 using Bitwise Operator class GFG { // function the return // multiply a number // with 4 using bitwise // operator static int multiplyWith4(int n) { // returning a number // with multiply with // 4 using 2 bit shifting // right return (n << 2); } // Driver Code public static void main(String[] args) { int n = 4; System.out.print(multiplyWith4(n)); } } // This code is contributed by Smitha. Python 3 # Python 3 program to multiply # a number with 4 using Bitwise # Operator # function the return multiply # a number with 4 using bitwise # operator def multiplyWith4(n): # returning a number with # multiply with 4 using2 # bit shifting right return (n << 2) # derive function n = 4 print(multiplyWith4(n)) # This code is contributed # by Smitha C# // C# program to multiply a number // with 4 using Bitwise Operator using System; class GFG { // function the return // multiply a number // with 4 using bitwise // operator static int multiplyWith4(int n) { // returning a number // with multiply with // 4 using 2 bit shifting // right return (n << 2); } // Driver Code public static void Main(String[] args) { int n = 4; Console.Write(multiplyWith4(n)); } } // This code is contributed by Smitha. PHP <?php // PHP program to multiply // a number with 4 using // Bitwise Operator // function the return // multiply a number // with 4 using bitwise // operator function 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. ?> JavaScript <script> // javascript program to multiply a number // with 4 using Bitwise Operator // function the return // multiply a number // with 4 using bitwise // operator function multiplyWith4(n) { // returning a number // with multiply with // 4 using 2 bit shifting // right return (n << 2); } // Driver Code var n = 4; document.write(multiplyWith4(n)); // This code is contributed by Amit Katiyar </script> Output16 Time Complexity: O(1)Auxiliary Space: O(1) 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. Comment More infoAdvertise with us Next Article Multiply any Number with 4 using Bitwise Operator S Shubham_Raj_Kumawat Follow Improve Article Tags : Bit Magic DSA Practice Tags : Bit Magic Similar Reads Check if a number is multiple of 9 using bitwise operators Given a number n, write a function that returns true if n is divisible by 9, else false. The most simple way to check for n's divisibility by 9 is to do n%9. Another method is to sum the digits of n. If sum of digits is multiple of 9, then n is multiple of 9. The above methods are not bitwise operat 5 min read Check if a number is divisible by 8 using bitwise operators Given a number n, check if it is divisible by 8 using bitwise operators. Examples: Input : 16 Output :YES Input :15 Output :NO Recommended PracticeCheck if a number is divisible by 8Try It! Approach: Result = (((n >> 3) << 3) == n). First we shift the 3 bit right then we shift the 3 bit 3 min read Check if a number is divisible by 17 using bitwise operators Given a number n, check if it is divisible by 17 using bitwise operators. Examples: Input : n = 34 Output : 34 is divisible by 17 Input : n = 43 Output : 43 is not divisible by 17 A naive approach will be to check it by % operator if it leaves a remainder of 0.To do division using Bitwise operators, 6 min read Check if a Number is Odd or Even using Bitwise Operators Given a number n, the task is to check whether the number is even or odd using Bitwise Operators.Examples: Input: n = 11 Output: OddInput: n = 10 Output: Even Following Bitwise Operators can be used to check if a number is odd or even:1. Using Bitwise XOR operator: The idea is to check whether the l 6 min read Computing INT_MAX and INT_MIN with Bitwise operations Prerequisites : INT_MAX and INT_MIN in C/C++ and Applications. Arithmetic shift vs Logical shiftSuppose you have a 32-bit system : The INT_MAX would be 01111111111111111111111111111111 and INT_MIN would be 10000000000000000000000000000000. 0 & 1 in most-significant bit position representing the 4 min read Like