Alternate bits of two numbers to create a new number | set even bit of number n ; res for store 101010. . number ; generate number form of 101010. . ... till temp size ; if bit is even then generate number and or with res ; return set even bit number ; set odd bit of number m ; res for store 101010. . number ; generate number form of 101010. . . . till temp size ; if bit is even then generate number and or with res ; return set odd bit number ; set even bit of number n ; set odd bit of number m ; take OR with these number ; Driver code ; n = 1 0 1 0 ^ ^ m = 1 0 1 1 ^ ^ result = 1 0 1 1
def setevenbits ( n ) : NEW_LINE INDENT temp = n NEW_LINE count = 0 NEW_LINE res = 0 NEW_LINE while temp > 0 : NEW_LINE INDENT if count % 2 : NEW_LINE INDENT res |= ( 1 << count ) NEW_LINE DEDENT count += 1 NEW_LINE temp >>= 1 NEW_LINE DEDENT return ( n & res ) NEW_LINE DEDENT def setoddbits ( m ) : NEW_LINE INDENT temp = m NEW_LINE count = 0 NEW_LINE res = 0 NEW_LINE while temp > 0 : NEW_LINE INDENT if not count % 2 : NEW_LINE INDENT res |= ( 1 << count ) NEW_LINE DEDENT count += 1 NEW_LINE temp >>= 1 NEW_LINE DEDENT return ( m & res ) NEW_LINE DEDENT def getAlternateBits ( n , m ) : NEW_LINE INDENT tempn = setevenbits ( n ) NEW_LINE tempm = setoddbits ( m ) NEW_LINE return ( tempn tempm ) NEW_LINE DEDENT n = 10 NEW_LINE m = 11 NEW_LINE print ( getAlternateBits ( n , m ) ) NEW_LINE