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 xnornum ; to 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 code
< ? php function xnor ( $ a , $ b ) { if ( $ a < $ b ) list ( $ a , $ b ) = array ( $ b , $ a ) ; if ( $ a == 0 && $ b == 0 ) return 1 ; $ a_rem = 0 ; $ b_rem = 0 ; $ count = 0 ; $ xnornum = 0 ; while ( $ a ) { $ a_rem = $ a & 1 ; $ b_rem = $ b & 1 ; if ( $ a_rem == $ b_rem ) $ xnornum |= ( 1 << $ count ) ; $ count ++ ; $ a = $ a >> 1 ; $ b = $ b >> 1 ; } return $ xnornum ; } $ a = 10 ; $ b = 50 ; echo xnor ( $ a , $ b ) ; ? >