Print numbers in the range 1 to n having bits in alternate pattern | Java implementation to print numbers in the range 1 to n having bits in alternate pattern ; function to print numbers in the range 1 to n having bits in alternate pattern ; first number having bits in alternate pattern ; display ; loop until n < curr_num ; generate next number having alternate bit pattern ; if true then break ; display ; generate next number having alternate bit pattern ; if true then break ; display ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static void printNumHavingAltBitPatrn ( int n ) { int curr_num = 1 , i = 1 ; System . out . print ( curr_num + " ▁ " ) ; while ( i != 0 ) { i ++ ; curr_num <<= 1 ; if ( n < curr_num ) break ; System . out . print ( curr_num + " ▁ " ) ; curr_num = ( ( curr_num ) << 1 ) ^ 1 ; if ( n < curr_num ) break ; System . out . print ( curr_num + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 50 ; printNumHavingAltBitPatrn ( n ) ; } }