|
I know this is a bit of a dumb question; but does C++ have an XOR operator
If not, can anyone remember enough boolean algebra to create an XOR out of AND, OR and NOT?
I should know, I only studied this stuff about 2 years ago, but heh, why not Harness The Power Of The Web (tm)
TIA,
Pete
|
|
|
|
|
Bitwise-exlcusive OR ^
Cheers
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
Thanks, but I need a non-bitwise operator, i.e. && || etc.
BTW, that was an impressive response time (3 minutes)
|
|
|
|
|
^ is the bitwise XOR operator, whereas ^^ is the logical XOR operator. If you don't know the difference, feel free to ask
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
there is no logical XOR. at least not in VC6
-c
Smaller Animals Software, Inc.
You're the icing - on the cake - on the table - at my wake. Modest Mouse
|
|
|
|
|
Oh yes, there ain't such an operator! I guess I just extended the obvious analogy from & and && without thinking it too much. What strikes me is that all these years have passed without noticing before Thanks for pointing out. My apologies to all.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
i wouldn't have known, if i wasn't looking for the exact thing last night. didn't find it, of course.
-c
Smaller Animals Software, Inc.
You're the icing - on the cake - on the table - at my wake. Modest Mouse
|
|
|
|
|
I found out that while answering a question to a VB programmer. He asked about XOR in C++, and I thought: "What a stupid question!", but I found out it was not that stupid after all.
I vote pro drink
|
|
|
|
|
That's what I like about answering questions here -- you have to relearn the stuff in order to give accurate answers, even when you get to the basics.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
VC++ spews at ^^
if( aCurrCPA[ref_c] ^^ aCurrCPA[temp_c] )
produces this error
D:\Pete\Capture\vss\Tracker.cpp(208) : error C2059: syntax error : '^'
is it non-standard C?
|
|
|
|
|
GEEK OUT ALERT!!!!
XOR via AND and OR:
(a & !b) | (b & !a)
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
Shouldn't it be
(a & ~b) | (b & ~a) not to mix logical and bitwise operators?
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
so for a logical operator this should work
(bool1 && !bool2) || (bool2 && !bool1)
right?
Thanks for the super-fast answers, BTW
|
|
|
|
|
In fact, if we want get all clever about it we could use
#define XOR( a, b ) ((a) && !(b)) || ((b) && !(a))
|
|
|
|
|
template <class T>
T XOR(const T val1, const T val2)
{
return ( ((val1)&(~val2)) | ((~val1)&(val2)) );
}
Hope that I am correct!
"Dirty hands lead to important discovery..." - Thomas Edison
|
|
|
|
|
For this to work, the class used has to implement the operators &,| and ~.
Otherwise you get comile errors (right?)
Good Luck,
Bill
|
|
|
|
|
This is more efficient (maximum number of evaluations is 2, in the previous formula it is 4)
bool1?!bool2:bool2
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
nice
|
|
|
|
|
But that isn't done with AND and ORs.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
So much stuff from such a little subject
Another advantage of a?!b:b with respect to (a&&!b)||(!a&&b) is that the former does not have the "double evaluation" problem of the latter, which is important if one plans to make a macro out of it.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
My version works better for circuits
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
If you're doing TTL (which means only NANDs and negators), I think this is the way to go (my electronics are a little rusty, nevertheless, I could be wrong)
(a NAND !b) NAND (!a NAND b)
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Yes that is logically the same thing. And since NAND is the primitive circuit that version is better.
Damn you Joaquin!!!!
(I just looked up half-adder in google and posted the first one I found.)
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
brilliant. you are a genius. damn i am still cursing myself for reaching this post and not thinking of it.
god... only a couple of hours here and i already love this site.
impuzible
|
|
|
|
|
Yup...
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|