We can multiply a number by 7 using bitwise operator. First left shift the number by 3 bits (you will get 8n) then subtract the original numberfrom the shifted number and return the difference (8n – n).
Time Complexity: O(1)
Space Complexity: O(1)
Note: Works only for positive integers.
Same concept can be used for fast multiplication by 9 or other numbers.
Improved By : vt_m
- Program to find whether a no is power of two
- Write a program to print all permutations of a given string
- Write an Efficient Method to Check if a Number is Multiple of 3
- Lucky Numbers
- Position of rightmost set bit
- Sum of bitwise OR of all subarrays
- Value in a given range with maximum XOR
- Check if a number can be expressed as 2^x + 2^y
- Find the missing element in an array of integers represented in binary format
- Finding the Parity of a number Efficiently