知道奇数的异或

时间:2018-09-08 21:04:57

标签: bit-manipulation bitwise-operators xor

给定一个由奇数或偶数组成的数组,一个人如何才能有效地获得XOR == 2的该数组对?

例如:

arr = [4,10,2,6,8]
pairs are: [(4,6), (8,10)] #4^6 == 2, 8^10 == 2

或者:

arr = [5,9,3,7,11]
pairs are: [(9,11), (5,7),]

我这样做是为了让他们(蛮力)

for i in combinations(inev,2):#inev is the list of indices (positions of the numbers in the array)

        if not (arr[i[0]] ^ arr[i[1]]) & 1 and (arr[i[0]] ^ arr[i[1]]) == 2:

                print arr[i[0]], arr[i[1]] #I print the pair 

0 个答案:

没有答案