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.
Because they allow greater precision and require fewer resources, bitwise operators can make some code faster and more efficient. Examples of uses of bitwise operations include encryption, compression, graphics, communications over ports/sockets, embedded systems programming and finite state machines.
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:
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
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
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.
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).
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).
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
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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With