We are a Number n and our task is multiply the number with 4 using bit-wise Operator.
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
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.
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.
- Multiply a number with 10 without using multiplication operator
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
- Russian Peasant (Multiply two numbers using bitwise operators)
- Find the remainder when N is divided by 4 using Bitwise AND operator
- Multiply a number by 15 without using * and / operators
- Compare two integers without using any Comparison operator
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Count pairs with equal Bitwise AND and Bitwise OR value
- Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N
- Check if bitwise AND of any subset is power of two
- Minimum Bitwise OR operations to make any two array elements equal
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array
- Efficient way to multiply with 7
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.