Associated Topics || Dr. Math Home || Search Dr. Math

### XOR Operation

```
Date: 01/23/2001 at 07:54:17
From: James
Subject: XOR

The bitwise operator XOR is defined as follows
0 XOR 1 = 1,   0 XOR 0 = 0,  1 XOR 1 = 0.
Thus if a = 1001 and b = 1101 then a XOR b = 0100

A 32-bit pattern is being transferred across a network. The four
bytes are labelled x1, x2, x3 and x4 where x4 = (x1 XOR x2) XOR x3.

If x1 = 01010101, x2 = 10110111, x3 = 00011101, then evaluate x4.

If a and b are 8-bit patterns (bytes) then, using your own data,
evaluate a XOR (b XOR a)
```

```
Date: 01/23/2001 at 11:40:23
From: Doctor Twe
Subject: Re: XOR

Hi James - thanks for writing to Dr. Math.

I take it that it's the XOR operation that's confusing you. Perhaps an
explanation and a few examples would help.

The exclusive-or (XOR) operation is like binary addition, except that
there is no carry from one bit position to the next. Thus, each bit
position can be evaluated independently of the rest.

Suppose we wanted to XOR 1100 and 0101. We could write these two
values below one another like an addition problem:

1100
0101
----

I'll start on the right side, like addition (though it really doesn't
matter with XORing which side you start at). In the rightmost bits we
have a 0 and a 1. 0 XOR 1 = 1, just like 0 + 1 = 1. So we write a 1 in
the rightmost bit of the result:

1100
0101
----
1

Next, we'll look at the second bit. We have a 0 and a 0. 0 XOR 0 = 0,
just like 0 + 0 = 0. So we write a 0 in the second bit of the result:

1100
0101
----
01

For the third bit, we have a 1 and a 1. 1 XOR 1 = 0, but unlike in
1 + 1 = 10, or 0 with a carry of 1.) So we write a 0 in the third bit
of the result:

1100
0101
----
001

Finally, in the leftmost bit, we have a 1 and a 0. 1 XOR 0 = 1, just
like 1 + 0 = 1. So we write a 1 in the rightmost bit of the result:

1100
0101
----
1001

So our final result is 1100 XOR 0101 = 1001. Do you see how that
works?

>A 32-bit pattern is being transferred across a network. The four
>bytes are labelled x1, x2, x3 and x4 where x4 = (x1 XOR x2) XOR x3.
>If x1 = 01010101, x2 = 10110111, x3 = 00011101 then evaluate x4.

Now you should be able to evaluate this. First, XOR the bits in x1
with the bits in x2, then XOR the result with the bits in x4.

>If a and b are 8-bit patterns (bytes) then, using your own
>data, evaluate a XOR (b XOR a)

Try some examples, and see what you come up with. Here's a hint: What
do you get when you XOR a byte with 00000000? What do you get when you
XOR a byte with 11111111? What do you get when you XOR a byte with
itself?

I hope this helps. If you have any more questions, write back.

- Doctor TWE, The Math Forum
http://mathforum.org/dr.math/
```
Associated Topics:
High School Calculators, Computers
High School Logic

Search the Dr. Math Library:

 Find items containing (put spaces between keywords):   Click only once for faster results: [ Choose "whole words" when searching for a word like age.] all keywords, in any order at least one, that exact phrase parts of words whole words

Submit your own question to Dr. Math
Math Forum Home || Math Library || Quick Reference || Math Forum Search