What is the logic of a bitwise XOR operator?  
Author Message
C McQuade





PostPosted: Visual C# General, What is the logic of a bitwise XOR operator? Top

Hello again experts,

I'm reading about BitArray's and a constant operation that is referenced is the Exclusive Or 'xor' op. I haven't been able to find an uncluttered (usually by SQL questions) explanation of what a logical xor looks like, little help




Visual C#9  
 
 
Naga Satish Rupenaguntla





PostPosted: Visual C# General, What is the logic of a bitwise XOR operator? Top

Here is the truth table for XOR

A

B

A XOR B

0

0

0

0

1

1

1

0

1

1

1

0

A XOR B = A’B + AB’

A' is the compliment of A. 0' is 1 and 1' is 0



 
 
PhilipRieck





PostPosted: Visual C# General, What is the logic of a bitwise XOR operator? Top

Well, you have two questsions there: bitwize XOR in your title, and logical XOR in the text. But bitwise is just a bit-by-bit extrapolation of logical, so...

Logical XOR is true if one and only one of the inputs is true, or said another way, logical XOR results in true if the two inputs are different.


For comparison, the familiar OR truth table looks like this when you have "x= a or b" (or in c#, "x=a || b;" assuming a,b and x are all booleans)

A B X (result)
------------------------
T F T
T T T
F F F
F T T

But an XOR looks like this:

A B X (result)
------------------------
T F T
T T F
F F F
F T T

XOR is used bitwise for a few tricks - for example, bitwise XORing any value with itself results in zero. (because each of the two bit patterns are the same). Another fun property - if you bitwse XOR the integer values A and B to get some number X, you can XOR X and A to get B back, or X and B to get A.

To see these yourself, I suggest writing pairs of numbers in binary and doing each bit by bit operation yourself (use small numbers ) For example

XOR 7(a) and 5(b) = x

0111 (7)
xor 0101 (5)
----
= 0010 (2)

to get b back, xor x and a (or 7 and b)

0111 (7)
xor 0010 (2)
----
0101 (5)


 
 
Axe22





PostPosted: Visual C# General, What is the logic of a bitwise XOR operator? Top

In simple english... if the two bits are the same you get FALSE, if they differ you get TRUE.

 When you XOR two numbers together, the result will have 1s in every bit that was different.

The only time you ever get a 0 when XORing two numbers is when their values are identical.