## Bitwise Binary OR AND XOR Clarification

FredCailloux
Posts: 69
Joined: Sat Mar 28, 2015 4:45 am

### Bitwise Binary OR AND XOR Clarification

Just a quick question in relation to logical operators.
C++ makes a distinction between Bitwise Binary and logical operators.
| is Bitwise OR & is Bitwise AND ~ is Bitwise NOT
|| is Binary OR && is Binary AND ! is Binary NOT
What about ^ Bitwise XOR Exclusive OR ? According to the Arduino reference it is a Bitwise operator
Most documentation concerning XOR ^ do not make the distinction between Bitwise or Binary.
What is the situation exactly ? Is ^ Bitwise and Binary ? Is there another character sign or letter for XOR Binary ?
Prashant Tripathi Knowing is to the Self what knowledge is to the ego

majenko
Posts: 2188
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

### Re: Bitwise Binary OR AND XOR Clarification

It is bitwise, but like all bitwise operators the result of the bitwise can be used as true/false.

Binary, like && and || can only operate on discrete true or false values. The fuzziness comes when you ask "what is true" and "what is false"?

In C a 0 is false, and any other value is true. So the value 3 is true. So with binary you have "3 && 8" would be true since both 3 and 8 are true. With bitwise "3 & 8" would be 0 since that is the bitwise result of AND-ing 3 and 8 (0b0011 & 0b1000 = 0b0000). So that would be false, not true. Hence the distinction between binary and bitwise.

With XOR it returns, of course, the XOR of the two values, as you would expect. But there is no equivalent XOR binary operator. Or is there...?

The truth table for XOR:

Code: Select all

``````A|B|Q
=====
0|0|0
0|1|1
1|0|1
1|1|0
``````
So it is false when A and B are the same, or true when A and B differ. So, you could say that:

Q is true if A is not equal to B.

Or...

Code: Select all

``````if (A != B) { ... }
``````
And that is why there is no actual XOR operator for binary in C like the bitwise operator. It is redundant, since "not-equal" performs the exact same job. Of course, that relies on A and B being actual single-bit binary values, otherwise it will compare the numbers in there. So you need to be a little cleverer:

Code: Select all

``````if (((A > 0) && (b == 0)) || ((A == 0) && B > 0))) { ... }
``````
Or, to make it smaller, you could use the ! operator to force an inverted binary value:

Code: Select all

``````if ((!A) != (!B)) { ... }
``````
Or, since we now have something that is either 0 or 1, since !3 is 0 and !0 is 1, you can:

Code: Select all

``````if ((!A) ^ (!B)) { ... }
``````
Cunning, huh? By reducing the values to 0 or 1 suddenly the bitwise and binary operators converge. They become identical.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

FredCailloux
Posts: 69
Joined: Sat Mar 28, 2015 4:45 am

### Re: Bitwise Binary OR AND XOR Clarification

Then I could use something like this

Code: Select all

``````#define ¨ !=
... some code
wr = (bit1 ¨ bit2 ) ;
... some more code``````
Can I?
( But I wont because then my code would become just a little more confusing
Prashant Tripathi Knowing is to the Self what knowledge is to the ego

majenko
Posts: 2188
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

### Re: Bitwise Binary OR AND XOR Clarification

No, you can't do that - ¨ is not a valid macro name.

You could do:

Code: Select all

``````#define XOR(A, B) ((!A) != (!B))

if (XOR(val1, val2)) { ... }
``````
That would cope with A and B being integers or whatever rather than just boolean.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".