XOR of all even numbers from a given range | Function to calculate XOR of numbers in the range [ 1 , n ] ; If n is divisible by 4 ; If n mod 4 is equal to 1 ; If n mod 4 is equal to 2 ; Function to find XOR of even numbers in the range [ l , r ] ; Update xor_r ; Update xor_l ; Driver Code
def bitwiseXorRange ( n ) : NEW_LINE INDENT if ( n % 4 == 0 ) : NEW_LINE INDENT return n NEW_LINE DEDENT if ( n % 4 == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n % 4 == 2 ) : NEW_LINE INDENT return n + 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def evenXorRange ( l , r ) : NEW_LINE INDENT xor_r = 2 * bitwiseXorRange ( r // 2 ) NEW_LINE xor_l = 2 * bitwiseXorRange ( ( l - 1 ) // 2 ) NEW_LINE return xor_l ^ xor_r NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 10 NEW_LINE r = 20 NEW_LINE print ( evenXorRange ( l , r ) ) NEW_LINE DEDENT