Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logic Proof of Associative Property for XOR

I came across a common programming interview problem: given a list of unsigned integers, find the one integer which occurs an odd number of times in the list. For example, if given the list:

{2,3,5,2,5,5,3}

the solution would be the integer 5 since it occurs 3 times in the list while the other integers occur even number of times.

My original solution involved setting up a sorted array, then iterating through the array: For each odd element I would add the integer, while for each even element I would subtract; the end sum was the solution as the other integers would cancel out.

However, I discovered that a more efficient solution existed by simply performing an XOR on each element -- you don't even need a sorted array! That is to say:

2^3^5^2^5^5^3 = 5

I recall from a Discrete Structures class I took that the Associate Property is applicable to the XOR operation, and that's why this solution works:

a^a = 0

and:

a^a^a = a

Though I remember that the Associative Property works for XOR, I'm having trouble finding a logical proof for this property specific to XOR (most logic proofs on the Internet seem more focused on the AND and OR operations). Does anyone know why the Associative Property applies to the XOR operation?

I suspect it involves an XOR identity containing AND and/or OR.

like image 316
Vilhelm Gray Avatar asked Nov 07 '12 14:11

Vilhelm Gray


People also ask

Does XOR have associative property?

XOR is useful because of four key properties: XOR has an identity element. XOR is self-inverting. XOR is associative.

Does XOR gate follow associative law?

The Exclusive-OR gates and equivalence gates both possess commutative and associative properties, and they can be extended to multiple input variables. For a multiple-input Ex-OR (XOR) gate output is low when even numbers of 1s are applied to the inputs, and when the number of 1s is odd the output is logic 0.

How do you prove associative property?

Proof of associativity We prove associativity by first fixing natural numbers a and b and applying induction on the natural number c. For the base case c = 0, (a+b)+0 = a+b = a+(b+0)

Does XOR follow distributive property?

XOR is not distributive over any operations (including itself). Out of all the bitwise operations only AND is distributive over all other bitwise operations such that (A & C) * (B & C) is equivalent to (A * B) & C where * is a bitwise operation.


2 Answers

The associative property says that (a^b)^c = a^(b^c). Since XOR is bitwise (the bits in the numbers are treated in parallel), we merely need to consider XOR for a single bit. Then the proof can be done by examining all possibilities:

abc (a^b) (a^b)^c (b^c) a^(b^c)
000   0      0      0      0
001   0      1      1      1
010   1      1      1      1
011   1      0      0      0
100   1      1      0      1
101   1      0      1      0
110   0      0      1      0
111   0      1      0      1

Since the third column, (a^b)^c, is identical to the fifth column, a^(b^c), the associative property holds.

like image 124
Eric Postpischil Avatar answered Oct 13 '22 00:10

Eric Postpischil


As long as a ^ b == ~a & b | a & ~b, you can proove that :

(a ^ b) ^ c = ~((~a & b) | (a & ~b)) & c | ((~a & b) | (a & ~b)) & ~c

and

a ^ (b ^ c) = a & ~((~b & c) | (b & ~c)) | ~a & ((~b & c) | (b & ~c))

Are equals.

like image 21
md5 Avatar answered Oct 13 '22 00:10

md5