human
Write me a code to print numbers in the range 1 to n having bits in alternate pattern and the other steps required given the following:
Print numbers in the range 1 to n having bits in alternate pattern | function to print numbers in the range 1 to nhaving 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.
Driven code"
How can the above be solved in Python?
assistant
#Here is code to print numbers in the range 1 to n having bits in alternate pattern and the other steps required.
 def printNumHavingAltBitPatrn ( n ) :
	curr_num = 1
	print ( curr_num )
	while ( 1 ) :
		curr_num = curr_num << 1 ;
		if ( n < curr_num ) :
			break ;
		print ( curr_num )
		curr_num = ( ( curr_num ) << 1 ) ^ 1 ;
		if ( n < curr_num ) :
			break
		print ( curr_num )
n = 50
printNumHavingAltBitPatrn ( n )