Find XOR of numbers from the range [ L , R ] | Function to return the XOR of elements from the range [ 1 , n ] ; If n is a multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to return the XOR of elements from the range [ l , r ] ; Driver code
function findxOR ( n ) { let mod = n % 4 ; if ( mod == 0 ) return n ; else if ( mod == 1 ) return 1 ; else if ( mod == 2 ) return n + 1 ; else if ( mod == 3 ) return 0 ; } function findXOR ( l , r ) { return ( findxOR ( l - 1 ) ^ findxOR ( r ) ) ; } let l = 4 , r = 8 ; document . write ( findXOR ( l , r ) ) ;