Given an integer N, the task is to print the first K multiples of N using Bitwise Operators.
Input: N = 16, K = 7
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
Input: N = 7, K = 10
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
Follow the steps below to solve the problem:
- Iterate up to K.
- For each iteration, print current value of N.
- Then, calculate the sum of 2i for every ith set bit of N. Add this sum to N and repeat from the step above.
Below is the implementation of the above approach:
16 * 1 = 16 16 * 2 = 32 16 * 3 = 48 16 * 4 = 64 16 * 5 = 80 16 * 6 = 96 16 * 7 = 112
Time Complexity: O(Klog2N)
Auxiliary Space: O(1)
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.
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Russian Peasant (Multiply two numbers using bitwise operators)
- Check if a number is multiple of 9 using bitwise operators
- Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
- Toggle case of a string using Bitwise Operators
- Check if a number is divisible by 17 using bitwise operators
- Check if a number is divisible by 8 using bitwise operators
- Check if a Number is Odd or Even using Bitwise Operators
- Find numbers which are multiples of first array and factors of second array
- Operators in C | Set 2 (Relational and Logical Operators)
- Bitwise right shift operators in Java
- Bitwise Operators in C/C++
- What are the differences between bitwise and logical AND operators in C/C++?
- 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
- Multiples of 3 or 7
- Multiples of 4 (An Interesting Method)
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.