Ankit Zalani
Ankit Zalani

Reputation: 3168

Significance of XOR operator

Hey can someone explain me what is the significance of XOR operator and what all problems can I solve using it. If someone can list which type of problems we can solve using XOR operator that would be very helpful.

Thanks in Advance.

Upvotes: 6

Views: 2409

Answers (2)

sameerkn
sameerkn

Reputation: 2259

Starting with Truthtable of XOR (^):

x  y    x^y
0  0    0    
0  1    1
1  0    1
1  1    0

Problem which can be solved using XOR:

  1. Comparison of 2 Boolean Functions x and y.

     If x and y are same then x ^ y = 0
     If x and y are different then x ^ y = 1
    
  2. Finding whether the number of ones ('1') in a binary representation of byte or integer is odd or even.

     unsigned char a = 10;  //in binary representation 00001010 (even number of 1s)
     unsigned char b = 11;  //in binary representation 00001011 (odd number of 1s)
     Simply XORing all the bits will give:
     * result = 1, if number of bits is odd
     * result = 0, if number of bits is even
    
  3. Using {Point 2.} the Parity (Parity Bit) of data bits can be found.

         If even parity is required(i.e the data bits should have even number of 1s) then 
         if all the bits are XORed and if it gives the result 1 then 
         **Parity Bit = 1** else **Parity Bit = 0**.  
         Similar case can be made if odd parity of data bits are required.
    
  4. In Proposition Logic if and only if (shortened iff) is a biconditional logical connective and this iff can be evaluated using XNOR or ~XOR (i.e negation of XOR).

  5. If a equation involving 2 Boolean Functions A and B such as {A'.B + A.B'} is encountered then this equation reduces to A ^ B. Solving {A'.B + A.B'} using primitive operators (AND(.), OR(+) and NEGATION(')) will result in 5 operations which can be reduced to 1 operation using XOR(^). Simply because A^B = A'.B + A.B'. If the equation encountered is {A'B' + AB} then {A'B' + AB} = ~XOR (i.e XNOR or negation of XOR).

  6. If a particular bit in data needs to be inverted (i.e 1 to 0 or 0 to 1) then simply XORing that bit with 1 would achieve the purpose.

        data = 1010;
               ^^^^
               0001  (inverting the LSB, first bit from right)
      ---------------
      result = 1011 
    

Upvotes: 8

Vedant Panchal
Vedant Panchal

Reputation: 75

Understanding XOR:

Note that:

A^B=A'.B+B'.A

also, as the name suggests

A^B=(A+B)%2

In simple terms, bitwise XOR gives 1 for same bits and 0 for different bits

General uses:

It can be used anywhere, where you can apply a logic such that equal components/pair of components, should annhilate.

e.g. a problem where you can to see which pair of shoes is incomplete. You're given type of shoes by integer. you can do:

xor=0

for n times:

xor^=shoe_type;

Where, ^ is an operator for XOR in most of the programming languages.

Here, only the integer which is in single quantity remains in our variable xor and the integers present in pair were evaluated to be zero. because a^a=0;

Upvotes: 1

Related Questions