Generate first K multiples of N using Bitwise operators Last Updated : 02 Nov, 2021 Comments Improve Suggest changes Like Article Like Report Given an integer N, the task is to print the first K multiples of N using Bitwise Operators. Examples: Input: N = 16, K = 7 Output: 16 * 1 = 16 16 * 2 = 32 16 * 3 = 48 16 * 4 = 64 16 * 5 = 80 16 * 6 = 96 16 * 7 = 112Input: N = 7, K = 10 Output: 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 Approach: 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: C++ // C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to print the first K // multiples of N void Kmultiples(int n, int k) { int a = n; for (int i = 1; i <= k; i++) { // Print the value of N*i cout << n << " * " << i << " = " << a << endl; int j = 0; // Iterate each bit of N and add // pow(2, pos), where pos is the // index of each set bit while (n >= (1 << j)) { // Check if current bit at // pos j is fixed or not a += n & (1 << j); // For next set bit j++; } } } // Driver Code int main() { int N = 16, K = 7; Kmultiples(N, K); return 0; } Java // Java program to implement // the above approach import java.util.*; class GFG{ // Function to print the first K // multiples of N static void Kmultiples(int n, int k) { int a = n; for (int i = 1; i <= k; i++) { // Print the value of N*i System.out.print(n+ " * " + i+ " = " + a +"\n"); int j = 0; // Iterate each bit of N and add // Math.pow(2, pos), where pos is the // index of each set bit while (n >= (1 << j)) { // Check if current bit at // pos j is fixed or not a += n & (1 << j); // For next set bit j++; } } } // Driver Code public static void main(String[] args) { int N = 16, K = 7; Kmultiples(N, K); } } // This code is contributed by Rohit_ranjan Python3 # Python3 program to implement # the above approach # Function to print the first K # multiples of N def Kmultiples(n, k): a = n for i in range(1, k + 1): # Print the value of N*i print("{} * {} = {}".format(n, i, a)) j = 0 # Iterate each bit of N and add # pow(2, pos), where pos is the # index of each set bit while(n >= (1 << j)): # Check if current bit at # pos j is fixed or not a += n & (1 << j) # For next set bit j += 1 # Driver Code N = 16 K = 7 Kmultiples(N, K) # This code is contributed by Shivam Singh C# // C# program to implement // the above approach using System; class GFG{ // Function to print the first K // multiples of N static void Kmultiples(int n, int k) { int a = n; for(int i = 1; i <= k; i++) { // Print the value of N*i Console.Write(n + " * " + i + " = " + a + "\n"); int j = 0; // Iterate each bit of N and add // Math.Pow(2, pos), where pos is // the index of each set bit while (n >= (1 << j)) { // Check if current bit at // pos j is fixed or not a += n & (1 << j); // For next set bit j++; } } } // Driver Code public static void Main(String[] args) { int N = 16, K = 7; Kmultiples(N, K); } } // This code is contributed by Amit Katiyar JavaScript <script> // javascript program to implement // the above approach // Function to print the first K // multiples of N function Kmultiples(n , k) { var a = n; for (i = 1; i <= k; i++) { // Print the value of N*i document.write(n+ " * " + i+ " = " + a +"<br>"); var j = 0; // Iterate each bit of N and add // Math.pow(2, pos), where pos is the // index of each set bit while (n >= (1 << j)) { // Check if current bit at // pos j is fixed or not a += n & (1 << j); // For next set bit j++; } } } // Driver Code var N = 16, K = 7; Kmultiples(N, K); // This code contributed by Princi Singh </script> Output: 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) Comment More infoAdvertise with us Next Article Generate first K multiples of N using Bitwise operators P PranjalKumar4 Follow Improve Article Tags : Bit Magic Mathematical Computer Science Fundamentals DSA setBitCount +1 More Practice Tags : Bit MagicMathematical 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 Multiply any Number with 4 using Bitwise Operator 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) 3 min read Bitwise Operations on Digits of a Number Given a number N, the task is to perform the bitwise operations on digits of the given number N. The bitwise operations include: Finding the XOR of all digits of the given number NFinding the OR of all digits of the given number NFinding the AND of all digits of the given number N Examples: Input: N 8 min read Bitwise Complement Operator (~ tilde) Pre-requisite:Bitwise Operators in C/ C++Bitwise Operators in JavaThe bitwise complement operator is a unary operator (works on only one operand). It takes one number and inverts all bits of it. When bitwise operator is applied on bits then, all the 1's become 0's and vice versa. The operator for th 3 min read Make n using 1s and 2s with minimum number of terms multiple of k Given two positive integer n and k. n can be represented as the sum of 1s and 2s in many ways, using multiple numbers of terms. The task is to find the minimum number of terms of 1s and 2s use to make the sum n and also number of terms must be multiple of k. Print "-1", if no such number of terms ex 6 min read Number of subarrays have bitwise OR >= K Given an array arr[] and an integer K, the task is to count the number of sub-arrays having bitwise OR ? K. Examples: Input: arr[] = { 1, 2, 3 } K = 3 Output: 4Bitwise OR of sub-arrays: { 1 } = 1 { 1, 2 } = 3 { 1, 2, 3 } = 3 { 2 } = 2 { 2, 3 } = 3 { 3 } = 3 4 sub-arrays have bitwise OR ? K Input: ar 15+ min read Generating N-bit Grey Code starting from K Given a value N and K, the task is to generate N-bits Gray Code starting from the value K.Examples: Input: N = 2, K = 3 Output: 3 2 0 1 Explanation: 3 -> 11 2 -> 10 0 -> 00 1 -> 01 Each value differ by only one bit from the next value in their binary representation.Input: N = 3, K = 2 Ou 4 min read Count valid Bitwise operation combinations for Binary Strings Given an odd integer N (N >= 3), a binary string S of length N and another string O of length (N-1)/2. Here, each character in string O can be one of the following: '&', '|' or '^' representing Bitwise operations AND, OR, and XOR respectively, the task is to find the number of possible combin 8 min read Find N distinct numbers whose bitwise Or is equal to K Given two integers N and K, the task is to find N distinct integers whose bit-wise OR is equal to K. If there does not exist any possible answer then print -1. Examples: Input: N = 3, K = 5 Output: 5 0 1 5 OR 0 OR 1 = 5Input: N = 10, K = 5 Output: -1 It is not possible to find any solution. Approach 10 min read Like