Exculsive OR. This is a binary operator. It returns true or 1 if only 1 of its conditions are true. With 3 or more input, true is returned if an odd number of inputs are true. In C, this is represented by ^.

This is equal to !(A&&B) && (A||B).

XOR, exclusive OR, is a logical function. Here is the truth table for Q = A XOR B:
  
  A|B|Q
  -+-+-
  0|0|0
  0|1|1
  1|0|1
  1|1|0

For more than two inputs, XOR returns 1 if and only if an odd number of its inputs are 1. This is required to maintain the associative property. Consider:
(1 xor 0) xor 0 => 1 xor 0 => 1
1 xor (0 xor 0) => 1 xor 0 => 1
(1 xor 1) xor 1 => 0 xor 1 => 1
1 xor (1 xor 1) => 1 xor 0 => 1
but:
(1 xor 0) xor 1 => 1 xor 1 => 0
1 xor (0 xor 1) => 1 xor 1 => 0

Exclusive OR is also sometimes abbreviated EOR.

See also other logic functions: AND, OR, NOT, NAND, NOR, NXOR.

The XOR operation can be used to swap two bit strings without using a third storage variable. To learn how, go to how to exhange two variables without using a third. It's fun!

1 XOR 1 = 0
0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 1 = 1


If we have two binary numbers "A" and "B" where:
A XOR B = C then:

C XOR A = B and C XOR B = A

For example:

101101110111011100 "A" XOR
101010001011100101 "B"
------------------
000111111100111001 "C" XOR
101010001011100101 "B"
------------------
101101110111011100 "A"



Coincidentally, This is also how microsoft encrypted their MS word documents awhile back. XOR'ing a 16 byte key throughout the document when the key can be extracted from the header of the file is not very smart.
I haven't checked to see if this is still how things are done, but if it is, Keep your eyes open for an example perl script

Also, xor was a primitive way to display sprites on a bitmap display when processor speed was low. By xoring the sprite, it would display, you could xor it again (thus erasing the sprite), and then xor the sprite somewhere else. A clever little hack.

A really good way to think about XOR is adding mod two and not "carrying" anything:

So:

  • 0 + 0 ~= 0 XOR 0 = 0
  • 0 + 1 ~= 0 XOR 1 = 1
  • 1 + 0 ~= 1 XOR 0 = 1
  • 1 + 1 (mod 2) ~= 1 XOR 1 = 0

This really helped in my understanding of XOR a little better, especially when working with LSFR's in cryptography.

XOR is (or was) a fast way to zero a register in assembly. For example, XOR AX,AX fills AX with 0's, because both sources are identical. This speeded the function by 1 clock or so in ancient x86 computers. Nowadays MOV AX,0x00 is as fast.

XOR encryption is considered the only mathematical proven way of non breakable encryption. It only works, if the key used for encryption is as long as the encrypted message and is being used only once. The key has to contain only hard random numbers.

This way of keeping secrets is called the one time pad.

If you fail to obey the rules above, the encryption is not safe anymore, since the standar cryptoanalytical methods can be applied to the encrypted text.

The soviets for instance used the one time pads after the WWII for the communication with their agents in the USA. They generated their random numbers by letting someone type at a typewriter at random. The persons who did this used onle one hand, hence typing only on the left or the right sides of the typewriter' keyboard. This little hook was enough to crack the cipher.

XON = X = xref

xor /X'or/, /kzor/ conj.

Exclusive or. `A xor B' means `A or B, but not both'. "I want to get cherry pie xor a banana split." This derives from the technical use of the term as a function on truth-values that is true if exactly one of its two arguments is true.

--The Jargon File version 4.3.1, ed. ESR, autonoded by rescdsk.

An internet services company based in Boulder, Colorado.

Excerpt from standard marketing propaganda:

Founded in 1991, XOR serves as a single point of accountability for building and running an eBusiness solution throughout all phases of its lifecycle, giving our clients the freedom to focus on what's really important to them – their core business.

XOR-Encryption

XOR is a bitwise operator and standard operation on bits and is called the 'eXclusive OR'.
XOR is true whenever an odd number of inputs is true.
In C-notation, XOR is written as ^ and in mathematical notation XOR is written as + with a circle around it.
Also note the following characteristics:

  • a XOR a = 0
  • a XOR b XOR b = a
This makes the encryption and decryption with one key (b) possible.
Let's take a look at the XOR-Encoding:
Let's assume we have a 3 letter key. We take the first letter of the plaintext and XOR it with the first letter of the key. Next, we take the second letter and XOR it with the second letter of the key. When we reach the fourth letter, we switch again to the first letter of the key. - We repeat the key.
An example:
"a" 01100001 XOR
"b" 01100010 =
00000011
We used the bitcode of ASCII 'a' as the plaintext and XORed it with the key ASCII 'b'.
Minding the fact that a XOR b XOR b = a, we can easily recover the plaintext:
"a xor b" 00000011 XOR
"b" 01100010 =
01100001 = ASCII "a"

In CMOS, XOR is carried out like:

                     _____
                       |
                   ____|____
             _   _|        _|
             A-||_     A-||_
                 _|    _   _|
             B-||_     B-||_
                  |_________|
                       |____________OUT
                     __|___          
               _   _|     _|      
               A-||_  B-||_
                    |______|
                     __|___          
               _   _|     _|      
               B-||_  A-||_
                    |______|
                     __|__
                      -|-
This, of course, is A XOR B, expressed as:
 _             _
(A * B) + (A * B)

A B | OUT
0 0 |  0
0 1 |  1
1 0 |  1
1 1 |  0

There is an interesting feature of the way XOR is defined for multiple inputs:

When ANDing multiple inputs, there is a "lazy" shortcut: if any of the inputs are 0, the output must also be 0, so there is no need to calculate the others.

Likewise, there is a shortcut for the OR gate. If any of the inputs are 1, the output must be 1; again, there is no need to resolve the other inputs.

The XOR gate has no such shortcut. Every single input must be calculated and tallied, as only an even number of TRUE inputs results in a TRUE return, and so any one input could reverse the output.

I'll add two more very simple, area-efficient, and fast MOSFET implementations of the XOR. The XOR is a critical part of many digital circuits, such as random number generators and adders. Therefore efficient XOR implementations are very desirable. The implementations below are in most ways superior to the brute-force CMOS implementations given in previous writeups.

OUT = A (XOR) B

              _
              B
              |
            -----
            -----
           |     |
 A----------     -----
                     |
                     |
                     |         OUT
              B      |-----------             
              |      |
            -----    |
            -----    |
 _         |     |   |
 A----------     -----
                               

The NMOSFETs are called "pass transistors"--they only pass the binary values on the left if they are on. The biggest problem with this pass transistor implementation is that NMOSFETs pass weak 1's (they pass the power supply voltage minus the NMOS threshold voltage). This problem can be rectified by adding PMOSFETs gated with opposite-polarity signals in parallel to the NMOSFETs. Such a parallel combination of an NMOSFET and PMOSFET is called a transmission gate. Notice that this XOR implementation does not consume static power--one of the MOSFETs is off at all times (assuming the inversion of B is instantaneous--in reality it will probably be lagged, which creates problems).

And another one (with rail to rail swing--notice that it doesn't require A to be inverted!)

OUT = A (XOR) B



       B
       |
       |
       |
      -               B
    ||                |
 A-o||                o
    ||              -----
      -             -----
       |    OUT    |     |
       |-----------|     |___ A
       |           |     |
      -            |     |
    ||              -----
 A--||              -----
    ||                |
      -               |
       |              _
       |              B
       |
       _ 
       B

Log in or register to write something here or to contact authors.