home left right up


Logical XOR/XNOR

Common Names: XOR, XNOR, EOR, ENOR

Brief Description

XOR and XNOR are examples of logical operators having the truth-tables shown in Figure 1.

Figure 1 Truth-tables for XOR and XNOR.

The XOR function is only true if just one (and only one) of the input values is true, and false otherwise. XOR stands for eXclusive OR. As can be seen, the output values of XNOR are simply the inverse of the corresponding output values of XOR.

The XOR (and similarly the XNOR) operator typically takes two binary or graylevel images as input, and outputs a third image whose pixel values are just those of the first image, XORed with the corresponding pixels from the second. A variation of this operator takes a single input image and XORs each pixel with a specified constant value in order to produce the output.

How It Works

The operation is performed straightforwardly in a single pass. It is important that all the input pixel values being operated on have the same number of bits in them, or unexpected things may happen. Where the pixel values in the input images are not simple 1-bit numbers, the XOR operation is normally (but not always) carried out individually on each corresponding bit in the pixel values, in bitwise fashion.

Guidelines for Use

We illustrate the function of XOR using




Since logical operators work more reliably with binary input we first threshold the two images, thus obtaining




Now, we can use XOR to detect changes in the images, since pixels which didn't change output 0 and pixels which did change result in 1. The image


shows the result of XORing the thresholded images. We can see the old and the new position of the moved object, whereas the stationary object almost disappeared from the image. Due to the effects of noise, we can still see some pixels around the boundary of the stationary object, i.e. pixels whose values in the original image were close to the threshold.

In a scene like


it is not possible to apply a threshold in order to obtain a binary image, since one of the objects is lighter than the background whereas the other one is darker. However, we can combine two grayscale images by XORing them in a bitwise fashion.


shows a scene where the dark object was moved and in


the light object changed its position. XORing each of them with the initial image yields




respectively. In both cases, the moved part appears at the old as well as at the new location and the stationary object almost disappears. This technique is based on the assumption that XORing two similar grayvalues produces a low output, whereas two distinct inputs yield a high output. However, this is not always true, e.g. XORing 127 and 128 yields 255. These effects can be seen at the boundary of the stationary object, where the pixels have an intermediate graylevel and might, due to noise, differ slightly between two of the images. Hence, we can see a line with high values around the stationary object. A similar problem is that the output for the moved pen is much higher than the output for the moved piece of paper, although the contrast between their intensities and that of the background value is roughly the same. Because of these problems it is often better to use image subtraction or image division for change detection.

As with other logical operators, XOR and XNOR are often used as sub-components of more complex image processing tasks. XOR has the interesting property that if we XOR A with B to get Q, then the bits of Q are the same as A where the corresponding bit from B is zero, but they are of the opposite value where the corresponding bit from B is one. So for instance using binary notation, 1010 XORed with 1100 gives 0110. For this reason, B could be thought of as a bit-reversal mask. Since the operator is symmetric, we could just as well have treated A as the mask and B as the original.

Extending this idea to images, it is common to see an 8-bit XOR image mask containing only the pixel values 0 (00000000 binary) and 255 (11111111 binary). When this is XORed pixel-by-pixel with an original image it reverses the bits of pixels values where the mask is 255, and leaves them as they are where the mask is zero. The pixels with reversed bits normally `stand out' against their original color and so this technique is often used to produce a cursor that is visible against an arbitrary colored background. The other advantage of using XOR like this is that to undo the process (for instance when the cursor moves away), it is only necessary to repeat the XOR using the same mask and all the flipped pixels will become unflipped. Therefore it is not necessary to explicitly store the original colors of the pixels affected by the mask. Note that the flipped pixels are not always visible against their unflipped color --- light pixels become dark pixels and dark pixels become light pixels, but middling gray pixels become middling gray pixels!

The image


shows a simple graylevel image. Suppose that we wish to overlay this image with its histogram shown in


so that the two can be compared easily. One way is to use XOR. We first use an image editor to enlarge the histogram until it is the same size as the first image. The result is shown in


To perform the overlay we simply XOR this image with the first image in bitwise fashion to produce


Here, the text is quite easy to read, because the original image consists of large and rather light or rather dark areas. If we proceed in the same way with


we obtain


Note how the writing is dark against light backgrounds and light against dark backgrounds and hardly visible against gray backgrounds. Compare the result with that described under OR. In fact XORing is not particularly good for producing easy to read text on gray backgrounds --- we might do better just to add a constant offset to the image pixels that we wish to highlight (assuming wraparound under addition overflow) --- but it is often used to quickly produce highlighted pixels where the background is just black and white or where legibility is not too important.

Interactive Experimentation

You can interactively experiment with this operator by clicking here.


  1. XOR



    Compare the result with the output of XORing their negatives. Do you see the same effect as for other logical operators?

  2. Use the technique discussed above to produce a cursor on

    Place the cursor on different location of the image and examine the performance on a background with high, low, intermediate and mixed pixel values.


R. Gonzalez and R. Woods Digital Image Processing, Addison-Wesley Publishing Company, 1992, pp 47 - 51.

E. Davies Machine Vision: Theory, Algorithms and Practicalities, Academic Press, 1990, Chap. 2.

B. Horn Robot Vision, MIT Press, 1986, pp 47 - 48.

Local Information

Specific information about this operator may be found here.

More general advice about the local HIPR installation is available in the Local Information introductory section.


home left right up

©2003 R. Fisher, S. Perkins, A. Walker and E. Wolfart.

Valid HTML 4.0!