Swap every two bits in bytes | Python program to swap every two bits in a byte . ; Extracting the high bit shift it to lowbit Extracting the low bit shift it to highbit ; driver Function
import math NEW_LINE def swapBitsInPair ( x ) : NEW_LINE INDENT return ( ( x & 0b10101010 ) >> 1 ) or ( ( x & 0b01010101 ) << 1 ) NEW_LINE DEDENT x = 4 ; NEW_LINE print ( swapBitsInPair ( x ) ) NEW_LINE