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 ] ; Stores XOR of even numbers in the range [ 1 , l - 1 ] ; Stores XOR of even numbers in the range [ 1 , r ] ; Update xor_r ; Update xor_l ; Driver Code
function bitwiseXorRange ( n ) { if ( n % 4 == 0 ) return n ; if ( n % 4 == 1 ) return 1 ; if ( n % 4 == 2 ) return n + 1 ; return 0 ; } function evenXorRange ( l , r ) { let xor_l ; let xor_r ; xor_r = 2 * bitwiseXorRange ( Math . floor ( r / 2 ) ) ; xor_l = 2 * bitwiseXorRange ( Math . floor ( ( l - 1 ) / 2 ) ) ; return xor_l ^ xor_r ; } let l = 10 ; let r = 20 ; document . write ( evenXorRange ( l , r ) ) ;