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 ?
Bitwise Binary OR AND XOR Clarification

 Posts: 69
 Joined: Sat Mar 28, 2015 4:45 am
 Location: Quebec  Canada
Bitwise Binary OR AND XOR Clarification
Prashant Tripathi Knowing is to the Self what knowledge is to the ego
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 ANDing 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:
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...
And that is why there is no actual XOR operator for binary in C like the bitwise operator. It is redundant, since "notequal" performs the exact same job. Of course, that relies on A and B being actual singlebit binary values, otherwise it will compare the numbers in there. So you need to be a little cleverer:
Or, to make it smaller, you could use the ! operator to force an inverted binary value:
Or, since we now have something that is either 0 or 1, since !3 is 0 and !0 is 1, you can:
Cunning, huh? By reducing the values to 0 or 1 suddenly the bitwise and binary operators converge. They become identical.
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 ANDing 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
ABQ
=====
000
011
101
110
Q is true if A is not equal to B.
Or...
Code: Select all
if (A != B) { ... }
Code: Select all
if (((A > 0) && (b == 0))  ((A == 0) && B > 0))) { ... }
Code: Select all
if ((!A) != (!B)) { ... }
Code: Select all
if ((!A) ^ (!B)) { ... }
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".
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".

 Posts: 69
 Joined: Sat Mar 28, 2015 4:45 am
 Location: Quebec  Canada
Re: Bitwise Binary OR AND XOR Clarification
Then I could use something like this Can I?
( But I wont because then my code would become just a little more confusing
Code: Select all
#define ¨ !=
... some code
wr = (bit1 ¨ bit2 ) ;
... some more code
( 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
Re: Bitwise Binary OR AND XOR Clarification
No, you can't do that  ¨ is not a valid macro name.
You could do:
That would cope with A and B being integers or whatever rather than just boolean.
You could do:
Code: Select all
#define XOR(A, B) ((!A) != (!B))
if (XOR(val1, val2)) { ... }
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".
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".