1

I need to know how to use XOR properly on more than two variables. I have following example.

a xor b xor c

Now, the way I understand it is that:

$a$ xor $b$ = $a$ * not $b$ + not $a$ * $b$

That part is fairly simple for me to understand but how does the next part work? I imagine it goes like this.

($a$ * not $b$ + not $a$ * $b$) xor $c$

So, how can I use XOR on more than one variable?

Squary94
  • 123
  • Just continue the same way you started. Put the entire parenthesis in place of a and c in place of b, and use De Morgan's laws to simplify. Either that, or treat xor as an elementary operation, its operation is much simpler to understand than what you are doing here. xoring just flips one operand if the other is 1. So you toggle a twice, with b and with c. Which boils down to the result of many xors to a 1 if the number of 1s is odd and 0 if it's even. – orion Jan 26 '16 at 23:15
  • 1
    An important point is that XOR is associative, so the result does not depend on whether you do a XOR b first or b XOR c first. AND and OR are the same, but not IF. – Ross Millikan Jan 26 '16 at 23:19

5 Answers5

4

I will use $\oplus$ for exclusive OR and $\bar x$ for NOT $x$. Just expand the expression:

$$\begin{align*} (x\oplus y)\oplus z&=(x\bar y+\bar xy)\oplus z\\ &=(x\bar y+\bar xy)\bar z+\overline{(x\bar y+\bar xy)}z\\ &=x\bar y\bar z+\bar xy\bar z+\overline{(x\bar y)}\;\overline{(\bar xy)}z\\ &=x\bar y\bar z+\bar xy\bar z+(\bar x+y)(x+\bar y)z\\ &=x\bar y\bar z+\bar xy\bar z+(\bar xx+\bar x\bar y+xy+y\bar y)z\\ &=x\bar y\bar z+\bar xy\bar z+(\bar x\bar y+xy)z\\ &=x\bar y\bar z+\bar xy\bar z+\bar x\bar yz+xyz \end{align*}$$

If you examine that final result, you’ll see that it has every product (conjunction) with an odd number of non-negated variables. This is true in general: $x_1\oplus x_2\oplus\ldots\oplus x_n$ is the sum (disjunction) of all expressions of the form $y_1y_2\ldots y_n$, where each $y_k$ is either $x_k$ or $\overline{x_k}$, and the number of $k\in\{1,\ldots,n\}$ such that $y_k=x_k$ is odd. Thus, for instance,

$$\begin{align*} w\oplus x\oplus y\oplus z&=wxy\bar z+wx\bar yz+w\bar xyz+\bar wxyz\\ &\quad+w\bar x\bar y\bar z+\bar wx\bar y\bar z+\bar w\bar xy\bar z+\bar w\bar x\bar yz\;. \end{align*}$$

This more general result can be proved by induction on the number of terms.

Brian M. Scott
  • 631,399
1

Since xor is associative (Prove XOR is commutative and associative?), there is no confusion when we write $$a \oplus b \oplus c = (a \oplus b) \oplus c,$$ which is what you've already found to be $$[(a \wedge \neg b) \vee (\neg a \wedge b)] \oplus c.$$ By the end, you get the ugly, yet valid $$\left[[(a \wedge \neg b) \vee (\neg a \wedge b)]\wedge \neg c\right] \vee \left[\neg[(a \wedge \neg b) \vee (\neg a \wedge b)] \wedge c\right].$$ I'm sure a few propositional equivalences could simplify that right up.

Why stop at three, though? You could go on and on via induction, until you have $$a \oplus b \oplus c \oplus d \oplus \dots = ((((a \oplus b) \oplus c) \oplus d) \oplus \dots)$$

And at the same time, it doesn't matter what you designate $a$, $b$, $c$, $d$, or any other variable to be because xor is commutative, as well!

0

Use

a xor b = a * not b + not a * b

again, but this time, in place of b put c and in place of a put

(a * not b + not a * b)

Browning
  • 659
0

NOTE: Just so you know, a XOR b is the same as $a \oplus b$. Also, here's a shortcut to XOR: If $a$ and $b$ are different, then $a \oplus b=1$. Otherwise, if they're the same, $a \oplus b=0$. Your formula works as a definition for XOR, but when you're calculating XOR in your head, I think this rule of thumb is easier.

If you know how to calculate $a \oplus b$, then you can simply calculate $a \oplus b$, set that to $d$ and then calculate $d \oplus c$. Basically, you work with two elements at a time: First, you look at the first two elements and then you take that result and use it with the third element.

Here's an example: Calculate $1 \oplus 0 \oplus 1$. Since $1$ and $0$ are different, we know that $1 \oplus 0=1$. This means that we now need to figure out $1 \oplus 1$. The first $1$ comes from the result of our first $\oplus$ calculation and the second $1$ comes from the fact that $1$ was the third element in our original expression. Since $1$ and $1$ are the same, we know that $1 \oplus 1=0$. Thus, $1 \oplus 0 \oplus 1=0$.

Noble Mushtak
  • 18,869
-1

Ensure that niether (b+a or b+c or a+c) are larger than your max datatype allows for. In other words in 16bit you can have XOR 255 and XOR (2^16 -256). This would be bit masking. Note the ^ is sometimes used for shifting instead of "to the power of".

Use XOR when trying to find the 2's Complement "the opposite of which bits have been set."

An example is 7 XOR 255 and may get you a byte of data for the number (248) 1111 1000 which is (15*16) + (8*1). However this is false if you dealing with 16 bit numbers 7 XOR 255 will be 0000 0000 1111 1000. Unless the byte order is swapped for 16 bit machines the same number might be stored as 1111 1000 0000 0000 which is very confusing. Like in 16 bit PCM data sounds. If you start multiplying these you might get unpredictable results especially if you start going beyond your MAX Integer value and mess around with "Casting" of datatypes. NEGATIVE 1 could be stored like 11111110

Search for "bit masking" and "two's complement" and karnaugh map.

What do you mean by not b? That it doesnt exist or that If B is true !B is false but do you mean the 2's complement? In order to find the twos complement you need all the bits to be set or 1s. You can do that with OR X where X has 1's for every bit. to set bits and AND0 to clear them.

Usually you need to know the number of bits used in your data type. And how negative numbers are stored in computers specifically your computer or raspberry pi or arduino or arm or motorolla processor or .... foobar named machine.

This technique is more common in low level programming "Assembly Languages".

Note: In decmial numbers we can multiply by 10 to append a zero in binary thats like multiplying by 2 and called a shift. If your left hand is in binary consider it times 16. So if you multiply a by b you could be shifting and so I disagree with the other answers on this page.

Back to 7 XOR 255 if you multiply both by 2 you would get problems 1110 XOR 1 1111 1110 this might set an overflow bit. I dont think XOR is reliable look on here for XOR Encryption and see problems especially in 64 bits and xxd command versus hexdump.

Punkroku
  • 1
  • 4