XNOR of two numbers | Python3 program to find XNOR of two numbers ; log ( n ) solution ; Make sure a is larger ; for last bit of a ; for last bit of b ; counter for count bit and set bit in xnor num ; for make new xnor number ; for set bits in new xnor number ; get last bit of a ; get last bit of b ; Check if current two bits are same ; counter for count bit ; Driver method
import math NEW_LINE def swap ( a , b ) : NEW_LINE INDENT temp = a NEW_LINE a = b NEW_LINE b = temp NEW_LINE DEDENT def xnor ( a , b ) : NEW_LINE INDENT if ( a < b ) : NEW_LINE INDENT swap ( a , b ) NEW_LINE DEDENT if ( a == 0 and b == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT a_rem = 0 NEW_LINE b_rem = 0 NEW_LINE count = 0 NEW_LINE xnornum = 0 NEW_LINE while ( a != 0 ) : NEW_LINE INDENT a_rem = a & 1 NEW_LINE b_rem = b & 1 NEW_LINE if ( a_rem == b_rem ) : NEW_LINE INDENT xnornum |= ( 1 << count ) NEW_LINE DEDENT count = count + 1 NEW_LINE a = a >> 1 NEW_LINE b = b >> 1 NEW_LINE DEDENT return xnornum ; NEW_LINE DEDENT a = 10 NEW_LINE b = 50 NEW_LINE print ( xnor ( a , b ) ) NEW_LINE