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 ; if num2 is greater then we swap this number in num1 ; Driver code
< ? php function togglebit ( $ n ) { if ( $ n == 0 ) return 1 ; $ i = $ n ; $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; return $ i ^ $ n ; } function XNOR ( $ num1 , $ num2 ) { if ( $ num1 < $ num2 ) list ( $ num1 , $ num2 ) = array ( $ num2 , $ num1 ) ; $ num1 = togglebit ( $ num1 ) ; return $ num1 ^ $ num2 ; } $ num1 = 10 ; $ num2 = 20 ; echo XNOR ( $ num1 , $ num2 ) ; ? >