问题
I a forum it is mentioned that given array of n
numbers:
arr[0........n-1]
The following conditon holds, ^
is the xor
operator `
f(l,r) = f(0,r) ^ f(0,l-1)
where f(l,r) = arr[l]^arr[l+1]^........arr[r]
I checked the above taking number of arrays and different values of l
and r
and YES, it is true. But I don't understand how?
Can somebody please explain the logic behind this?
回答1:
Use the simplest property of XOR
f(0,r) ^ f(0,l-1) = f(l,r)
=> (f(0,r) ^ f(0,l-1)) ^ f(0,l-1) = f(0,l-1) ^ f(l,r)
=> f(0,r) = f(l,r) ^ f(0,l-1) [Since XOR is associative f(0,l-1) ^ f(0,l-1) = 0 and x ^ 0 = x]
=> f(0,r) = (arr[0]^...arr[l-1])^(arr[l]^...^arr[r])
which is definition of f(0,r)
.
回答2:
The numbers arr[0...r] can be seen as two parts arr[0...l-1], arr[1...r] so f(0,r) = f(0,l-1) ^ f(l,r) , by treating the two parts separately.
In f(l,r) = f(0,r) ^ f(0,l-1) the f(0,l-1) cancels with the f(0,l-1) in f(0,r) leaving f(l,r) behind.
来源:https://stackoverflow.com/questions/38833308/can-somebody-explain-the-following-xor-property