Generate first K multiples of N using Bitwise operators | Java program to implement the above approach ; Function to print the first K multiples of N ; Print the value of N * i ; Iterate each bit of N and add Math . pow ( 2 , pos ) , where pos is the index of each set bit ; Check if current bit at pos j is fixed or not ; For next set bit ; Driver Code
import java . util . * ; class GFG { static void Kmultiples ( int n , int k ) { int a = n ; for ( int i = 1 ; i <= k ; i ++ ) { System . out . print ( n + " ▁ * ▁ " + i + " ▁ = ▁ " + a + "NEW_LINE"); int j = 0 ; while ( n >= ( 1 << j ) ) { a += n & ( 1 << j ) ; j ++ ; } } } public static void main ( String [ ] args ) { int N = 16 , K = 7 ; Kmultiples ( N , K ) ; } }