XNOR of two numbers | python program to find XNOR of two numbers ; Please refer below post for details of this function https : www . geeksforgeeks . org / toggle - bits - significant - bit / ; Make a copy of n as we are going to change it . ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Returns XNOR of num1 and num2 ; Make sure num1 is larger ; Driver code
import math NEW_LINE def togglebit ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT i = n NEW_LINE n = n | ( n >> 1 ) NEW_LINE n |= n >> 2 NEW_LINE n |= n >> 4 NEW_LINE n |= n >> 8 NEW_LINE n |= n >> 16 NEW_LINE return i ^ n NEW_LINE DEDENT def xnor ( num1 , num2 ) : NEW_LINE INDENT if ( num1 < num2 ) : NEW_LINE INDENT temp = num1 NEW_LINE num1 = num2 NEW_LINE num2 = temp NEW_LINE DEDENT num1 = togglebit ( num1 ) NEW_LINE return num1 ^ num2 NEW_LINE DEDENT a = 10 NEW_LINE b = 20 NEW_LINE print ( xnor ( a , b ) ) NEW_LINE