No conditional XOR in C++/C#?
-
I've always wondered why there is no XOR in C++, and again in C#? There is the Logical XOR (^), but no "^^"
XOR doesn't makes sense in any useage other than a bitwise XOR. How would this work? If (true ^^ false) DoSomething(); what would be the result? And how would it differ from: if (true ^ false) ?
-
XOR doesn't makes sense in any useage other than a bitwise XOR. How would this work? If (true ^^ false) DoSomething(); what would be the result? And how would it differ from: if (true ^ false) ?
What do u mean? I want to check if something changed for example.. I have a bool value, and if that value changes, I want to take action. What u can do is: bool b=m_mybool; m_mybool=GetBoolNewValue(); [...] if(b ^^ m_mybool) { Do something or another. } To answer your question: if(true ^ false) is exactly the same as if(true ^^ false). However, if(a ^ b) is not the same as if(a ^^ b) - remember - any non zero value is true - a ^ b is the same as a ^^ b only when both a and b have only 1 and 0 as values. The previous example I showed would have worked just as well if I had written if(b != m_mybool). HOWEVER, this is true ONLY when the types are of type bool - had the types been of type int or char* or some such, I would not have been able to do that. If, for example, the check I wanted to do is : "if only one of the strings is a valid string", I have to resort to if(((st1==NULL) && (st2!=NULL)) || ((st1!=NULL) && (st2==NULL))). Had I had the operator ^^, I could have simply written: if(st1 ^^ st2). Get it? Shahar
-
What do u mean? I want to check if something changed for example.. I have a bool value, and if that value changes, I want to take action. What u can do is: bool b=m_mybool; m_mybool=GetBoolNewValue(); [...] if(b ^^ m_mybool) { Do something or another. } To answer your question: if(true ^ false) is exactly the same as if(true ^^ false). However, if(a ^ b) is not the same as if(a ^^ b) - remember - any non zero value is true - a ^ b is the same as a ^^ b only when both a and b have only 1 and 0 as values. The previous example I showed would have worked just as well if I had written if(b != m_mybool). HOWEVER, this is true ONLY when the types are of type bool - had the types been of type int or char* or some such, I would not have been able to do that. If, for example, the check I wanted to do is : "if only one of the strings is a valid string", I have to resort to if(((st1==NULL) && (st2!=NULL)) || ((st1!=NULL) && (st2==NULL))). Had I had the operator ^^, I could have simply written: if(st1 ^^ st2). Get it? Shahar
I agree that it seems a little strange to not throw in a ^^ operator, but I don't think I would use it very often if it did exist. Maybe its just not a very common logic operation for the common programmer. If you find yourself needing one a lot, its a good thing c++ is so extensible: #define XOR(a,b) (!(((a) && (b)) || (!(a) && !(b)))) thats not as pretty as ^^ but i think it works.
-
I've always wondered why there is no XOR in C++, and again in C#? There is the Logical XOR (^), but no "^^"
Because != is the same as XOR for logical types:
if ((a < 1) != (b > 2))
returnstrue
if one and only one of the two statements is true.