I know how to use AND & OR but I don't know how and when to use XOR. I know that 1 XOR 1=0, 0 XOR 1=1 and so forth, but that doesn't help me much, I can't "see" the bits in front of me the same way I do when using OR and AND. Could someone give me a good XOR example so I can gain deeper knowledge of the XOR operation?
Posted on 2003-10-23 14:00:58 by Delight
Erm... you use it when you want some bits that are 1 to become 0, and bits that are 0 to become 1. It's useful when you want to negate part of a number, or toggle some flags in a flag variable.
Posted on 2003-10-23 14:14:18 by Sephiroth3
Or, for instance, if you need to know, which bits are different:

XOR AL,BL:
10101010=AL before
10101001=BL before
-------------
00000011=AL after

"NOT operand" performs the same operation as "XOR operand,-1".
Posted on 2003-10-23 14:54:18 by MazeGen
I use it mainly to flip flags around ;)

xor , TRUE

Makes False become True and True become False.
Posted on 2003-10-23 15:09:54 by JimmyClif
``````
XOR Table
-----------------------------
Start | XORed with | Result
-----------------------------
0     | 1          | 1
0     | 0          | 0
1     | 0          | 1
1     | 1          | 0

OR

-----------------------------
Start | ORed with  | Result
-----------------------------
0     | 1          | 1
0     | 0          | 0
1     | 0          | 1
1     | 1          | 1
``````
Posted on 2003-10-23 15:30:59 by devilsclaw
ANDing bits with 0 clears them (to 0)
ORing bits with 1 sets them (to 1)
NOT 10010000 toggles all the bits to 01101111 (1's complement)
NOT is not the same as NEG (which is used to change signs)

XORing bits with 1 toggles them

Say, if i want to toggle the states of bits 4, 5, and 7, i would use the following

10101000
10110000 XOR
============
00011000 =

which simply indicates XORing using 1 toggles the state of bits.

XOR has another purpose, to clear a register to 0
for example,

XOR eax, eax

is equivalent to

MOV eax, 0

since 1 XOR 1 = 0
and
0 XOR 0 = 0
Posted on 2003-10-23 15:37:38 by art_sands
For the most part, the result is the unmatch bit positions = 1, matched bit positions = 0. It a great operator to find what is different in operands. Good for testing state of flags or software conditions.

Beside zeroing a register like, XOR EAX,EAX.

Regards, P1 :cool:
Posted on 2003-10-23 15:41:20 by Pone
A couple other ways of viewing XOR.

1) As logic statement

The normal Boolean OR is the inclusive OR: either A is true or B is true or both.

With two operands, the exclusive OR rejects the previous last case: either A is true or B is true but not both.

XOR is the same as binary addition except the carry is ignored.

0 + 0 = 0, 0 XOR 0 = 0
0 + 1 = 1, 0 XOR 1 = 1
1 + 0 = 1, 1 XOR 0 = 1
1 + 1 = (1)0, 1 XOR 1 = 0

AND to extract bits with a bit mask
AND to clear bits with a complemented bit mask
OR to set bits with a bit mask
XOR to complement bits with a bit mask

Some algorithms, like CRC calculation, use XOR simply as a function without any reference to its other meanings.
Posted on 2003-10-23 17:52:19 by tenkey
Logic operators are operand who decide if the door whil open or not

In the case of XOR
Think about two corridors who bring to one door.
each corridor have a detector to know if we have someone inside
the rule is dont open the door if 2 guy there

MOV EAX,
XOR EAX,

EAX = 1 If only one guy there....

I hope that is helping you a bit .....

0100000100101011
Posted on 2003-10-23 20:17:33 by BinBin
Another way of seeing XOR:

XOR reverses the bits in the destination that are set in the source.
``````FOR i = 0 TO 31
IF bit(source, i)
bit(destination, i) = NOT bit(destination, i)
ENDIF
NEXT i``````
...viewing it this way really helps optimization of some algorithms.
Posted on 2003-10-23 21:46:54 by bitRAKE
I like thinking of it by just what its name means, Exclusive.

The "Exclusive" OR operation is best used to identify differences between two sources that are Exclusive between themselfs. This means "report back a value of 1 if either register is different, while evaluating it on a bit level".

When applied to things like "XOR EAX, EAX" you end up with EAX = 0 because there is nothing EXCLUSIVELY DIFFERENT between a direct copy of the the source.

As pointed out earlier, this nature has good use is bit flipping. For example, if you wanted to Toggle the value of the 3rd bit in a register EDX, you would use a mask pattern of:

"00000000-00000000-00000000-00000100b"

At a binary level, the only thing that is EXCLUSIVELY DIFFERENT from '0' is '1'. If two bits are exclusively different, the definition says to produce a '1' indicating this. So in essence where a 0 bit is used in the mask, to get a result of '1' the only mutially exclusive value from the mask is '1'. If it was not exclusively different (equal) its definition says to produce a '0', which as well mirrors the only possible value of '0' when the mask is zero, '0' !

Where ever a mask bit of '1' ocours, the end result will simply negate instead of mirror the bit in evaluation. Since '1' is now in the mask, to get a result of '1' the only MUTUALLY DIFFERENT bit to produce this result would then be '0' (Which is opposite from each other). Likewise, to get a result of '0' the only value that ISNT mutually different is then a '1'. (Again opposite from each other).

Another case is the "switch" routine:

If EAX = 0F0Fh and EBX = 1234h Then the following code:

xor EAX, EBX
xor EBX, EAX
xor EAX, EBX

Will result in EAX = 1234h and EBX = 0F0Fh! Dont believe me? Well i will show you why: ( ^ = XOR )

 EAX* = EAX ^ EBX
 EBX* = EBX ^ EAX* = EBX ^ (EAX ^ EBX) = EBX ^ (EBX ^ EAX) = (EBX ^ EBX) ^ EAX = (0) ^ EAX = EAX
 EAX** = EAX* ^ EBX* = (EAX ^ EBX) ^ EAX = (EBX ^ EAX) ^ EAX = EBX ^ (EAX ^ EAX) = EBX ^ (0) = EBX

In the end, EBX* = EAX and EAX** = EBX.

(1) This is because EAX* is the result of EAX with additional bits set that is MUTUALLY EXCLUSIVE TO EBX only! EAX* is now mutated with the essence of EBX packed in to iself as well!!.

(2) EBX* does the same, but with the 'mutated' source of EAX*. Seeing this time around EAX* is already packed with all things unique to EBX, the XOR operation finds NOTHING mutually exclusive to EBX in EAX* and as a result negates the mutation. Thus EBX* = EAX.

(3) The previous two operations essentially moved EAX -> EBX. But we still have EAX* which is the mutated version of EAX and EBX we started with. To correct for this a similar process as (2) above is done to produce EAX**. EAX** = EAX* ^ EBX*. Since EBX* is now the origional EAX. The only thing mutually eclusive from the mutated combination of EAX / EBX and EAX itself would be EBX!.

I thought this would be an easy post, however, writing out how i think has been surprisingly challenging in this area. ( :P )

Regards,
:alright:
:NaN:
Posted on 2003-10-23 23:32:15 by NaN
XOR can be used to create "parity"-disks data (RAID 3 and/or/xor (;)) 5, iirc).

Because:
A XOR B = C ; 10101010 XOR 11110000 = 01011010
A = B XOR C ; 10101010 = 11110000 XOR 01011010
B = A XOR C ; 11110000 = 10101010 XOR 01011010

So if A, B or C is "lost", you can 'caluclate' the lost part by xoring the know values togeter.

``````just had little extra time, so I too write the truth table for xor:
Q = A XOR B  ( Q = A (+) B, (+) = a plus sign in a circle)
A|B|Q
-+-+-
0|0|1
0|1|0
1|0|0
1|1|0``````
Posted on 2003-10-23 23:44:42 by scientica
Thanks guys! You have all done a great job explaining how it works and I understand now :alright:
Posted on 2003-10-24 07:18:15 by Delight
All folks,

The information that all of you provided here was by far excellent on the topic. Would you mind contributing in such small amounts to a book on assembly language and it's use in win32 and linux? I've been trying to structure such a book. The answer to one's question in a forum such as this just keeps getting better and better. So, Hiroshimator figured out that we could use wiki (a collaboration kind of forum where you can edit the content online) to create the book.

The big problem has been, as Randy pointed out, people want stuff from us, but they themselves don't want to share. That's a little unfair, isn't it? After all, the book is for them and, as another matter of fact, free for online reading. So, I encourage you to think about it. Just imagine the quality of the book an entire community could produce. Get in touch with me at the "Let's make a book out of this kb" thread. I sincerely hope all of you generous people out there will do something about it.

Regards,
Art
Posted on 2003-10-24 07:46:04 by art_sands