How to swap two bits in a given integer ? | Python code for swapping given bits of a number ; left - shift 1 p1 and p2 times and using XOR ; Driver Code
def swapBits ( n , p1 , p2 ) : NEW_LINE INDENT n ^= 1 << p1 NEW_LINE n ^= 1 << p2 NEW_LINE return n NEW_LINE DEDENT print ( " Result ▁ = " , swapBits ( 28 , 0 , 3 ) ) NEW_LINE