human
Give me a code to toggle first and last bits of a number and the other steps required given the following:
Toggle first and last bits of a number | Returns a number which has same bit count as n and has only first and last bits as set ..
set all the bit of the number.
Adding one to n now unsets all bits and moves MSB to one place . Now we shift the number by 1 and add 1..
if number is 1.
take XOR with first and last set bit number.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to toggle first and last bits of a number and the other steps required.
 def takeLandFsetbits ( n ) :
	n = n | n >> 1
	n = n | n >> 2
	n = n | n >> 4
	n = n | n >> 8
	n = n | n >> 16
	return ( ( n + 1 ) >> 1 ) + 1
def toggleFandLbits ( n ) :
	if ( n == 1 ) :
		return 0
	return n ^ takeLandFsetbits ( n )
n = 10
print ( toggleFandLbits ( n ) )