# XOR two numbers

Is there an intuitive meaning of XOR of two numbers not involving binary and just decimal? Or is is always converted into binary and then XORed?

• Depends on your intuition. There's clearly a computable function $\mbox{XOR}:\mathbb{N}^2\rightarrow\mathbb{N}$ such that $\mbox{XOR}(n,m)$ is the exclusive-or of $n$ and $m$, regardless of the base you represent them in. Binary's the most intuitive to me, but your intuitive mileage may vary. And binary's pretty clearly the most straightforward way to describe an algorithm for computing $\mbox{XOR}$. So if that's what you mean by "intuition", then binary's likely best. – John Forkosh Feb 20 '16 at 3:43

Bitwise operations like (bitwise) AND, OR, and XOR don't make much sense from the perspective of decimal expansion. They do make some sense in bases which are powers of 2 like hexadecimal, since in such bases they also operate digit by digit.

• So it is possible to XOR two numbers (in Hex) without converting it into it's binary representation? – user2505282 Feb 20 '16 at 1:05
• @user2505282 Yes. You do it digit by digit. – Yuval Filmus Feb 20 '16 at 9:20
• Can you give me an example? – user2505282 Feb 20 '16 at 9:27
• 34 xor 43 = 77. You need to know how to XOR two digits. – Yuval Filmus Feb 20 '16 at 9:36
• No, that's false. 53 xor 75 = 26. The point is that this reduces to two independent calculations: 5 xor 7 = 2 and 3 xor 5 = 6. – Yuval Filmus Feb 20 '16 at 9:39

There are only 16 distinct binary operations possible for $a$ op $b$, i.e., $0, 1, a, b, \overline{a}, \overline{b}, ab, a\overline{b}, \overline{a}b, \overline{a}\overline{b}, a+b, a+\overline{b}, \overline{a}+b, \overline{a}+\overline{b},ab+\overline{a}\overline{b}$ and $a\overline{b}+\overline{a}b$. XOR is $a\overline{b}+\overline{a}b$.

With decimal digits you have a staggering possibility of $10^{100}$ functions. However, the idea of XOR can be easily duplicated. Nevertheless the idea of doing bitwise XOR is not acceptable, 0000 $\oplus$ 0000 is 1111 which is 15, not a decimal digit.

Let us try to do something like XOR for tertiary digits, i.e. 0,1,2. What we want is this: if $a$ XOR $b$ is $c$, then $a$ XOR $c$ should be $b$ and $b$ XOR $c$ should be $a$. So we should have the following:

$a \oplus b$ = $c$ $\Leftrightarrow$ $a \oplus c$ = $b$ $\Leftrightarrow$ $b \oplus c$ = $a$, for every choice of $a,b, c \in \{0, 1, 2\}$.

Also we would like to have $a \oplus b = b \oplus a$.

With this in mind we can have the following table:

XOR | 0 1 2
-----------
0   | 0 2 1
1   | 2 1 0
2   | 1 0 2


And we can have yet another table

XOR | 0 1 2
-----------
0   | 1 0 2
1   | 0 2 1
2   | 2 1 0


If we want to define XOR for decimal preserving the above mentioned property we can do it similarly. Of course, there wont be a unique way to this. And since there is no unique way to do this, there is no standard definition of XOR for decimal digits. It is cook your own definition kind of thing.

No, there is no intuitive meaning in terms of decimal. Bitwise operations are defined (literally) as operations on bits, and bits don't correspond directly to decimal digits.

XOR does have meaning on how decimal numbers are stored especially if you are considering using signed decimal notation. I think of XOR because it is useful in calculations requiring the 2's Complement.

You need to learn how negative numbers are stored in computers and also consider instances where Big Endian and Little Endian are used when testing your code.

• Search for "Karnaugh Maps" "Twos Complement" and "Max Data Type" – Ember Leona Nov 5 '18 at 4:54

Normal "or" operation contains "and +or" (means we also treat the both true cases as true which is and) but this doesn't gives rise to all possible combinations. For example consider there is one red and one white box, which contain balls of a certain colour. If the box contains same colour ball (eg., red box-red ball) let's denote that by bit 1 and denote all other cases by bit 0. We want to know whether only one box contains the same colour ball or both. In this case the normal "or" operation can't give the required output so we use exclusive or "xor" operation. Xor is used to compare if two signals (inputs) have correlation (similarity). So the physical meaning is not decimal or hexadecimal format the input is. It will always be converted to binary format to compare the similarity between two numbers. In a nutshell mainly tells the similarity between two things.

• The question is asking whether xor has any particular meaning on decimal numbers. Most of your answer explains what xor does on binary numbers, which isn't really relevant. And, xor doesn't seem to have much to do with similarity of decimal numbers. For example, $1024\oplus1$ has only a couple of bits set, suggesting that $1024$ and $1$ are "similar" numbers, whereas $1024\oplus 1023$ has ten bits set, suggesting they're very dissimilar. But I think most people would agree that $1023$ and $1024$ are very similar, whereas $1$ and $1024$ are very different. – David Richerby Feb 20 '16 at 17:21