human
Swap bits in a given number | .
Setting bit at p1 position to 1.
Setting bit at p2 position to 1.
value1 and value2 will have 0 if num at the respective positions - p1 and p2 is 0..
check if value1 and value2 are different i . e . at one position bit is set and other it is not.
if bit at p1 position is set.
unset bit at p1 position.
set bit at p2 position.
if bit at p2 position is set.
set bit at p2 position.
unset bit at p2 position.
return final result.
Driver code"
Solution in Python:
 ... Write me a code to swap bits in a given number and the other steps required?
assistant
#Here is code to swap bits in a given number and the other steps required.
 def swapBits ( num , p1 , p2 , n ) :
	shift1 = 0
	shift2 = 0
	value1 = 0
	value2 = 0
	while ( n > 0 ) :
		shift1 = 1 << p1
		shift2 = 1 << p2
		value1 = ( ( num & shift1 ) )
		value2 = ( ( num & shift2 ) )
		if ( ( value1 == 0 and value2 != 0 ) or ( value2 == 0 and value1 != 0 ) ) :
			if ( value1 != 0 ) :
				num = num & ( ~ shift1 )
				num = num | shift2
			else :
				num = num & ( ~ shift2 )
				num = num | shift1
		p1 += 1
		p2 += 1
		n -= 1
	return num
res = swapBits ( 28 , 0 , 3 , 2 )
print ( " Result ▁ = " , res )