Can somebody explain the following xor property

半世苍凉 提交于 2019-12-12 12:05:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!