 |
|
 |
logical (a xor b)
could be in c/c++ this way
a != b
a and b are bool type
Miguel PS
|
|
|
|
 |
|
 |
Correct, but, if a and b aren't bool, you'll get an incorrect behaviour without warning...
- Goran.
|
|
|
|
 |
|
 |
...it just lacks enterprise. Here is the enterprise way of doing this (enterprise operator precedence problems included):
#define log_xor !=0==!
|
|
|
|
 |
|
 |
Not bad.
- Goran.
|
|
|
|
 |
|
 |
I don't like switching-off warnings like this on the general principle that they're actually useful sometimes. I prefer in this case to use the != operator to explicitly "cast to a bool":
template <typename T1, typename T2>
bool logical_xor( T1 p1, T2 p2 )
{
return (p1 != 0) ^ (p2 != 0);
}
|
|
|
|
 |
|
 |
Neat implementation but seems like a lot of trouble to avoid typing !a != !b which is exactly the same operation.
|
|
|
|
 |
|
 |
!0 is not guaranteed to always be the same value, it is only guaranteed to be non-zero. So !0 != !0 might be erroneously true.
|
|
|
|
 |
|
 |
Alistair Milne wrote: !0 is not guaranteed to always be the same value, it is only guaranteed to be non-zero. So !0 != !0 might be erroneously true.
I don't know what you mean. !0 is always 1 or true in C/C++.
The result of a logical operator is always a boolean. So !x will always return 0 (false) if x!=0 or 1 (true) if x==0.
!a != !x will be true iff a==0 and x != 0 or a!=0 and x==0. That is the logical XOR operator.
|
|
|
|
 |
|
 |
!0 is only guaranteed to be non-zero, it is not guaranteed to be always the same integer value, your code is not portable. C doesn't even have a bool type, !0 is an int in C.
|
|
|
|
 |
|
 |
I thought "!=" was the logical xor operator (think about it) ...
David F
|
|
|
|
 |
|
 |
Sorry, but no. 1 != 2 is true 1 log_xor 2 is false - Goran.
|
|
|
|
 |
|
 |
I think that there is a logical xor in C++ which is denoted by the ^ operator.
|
|
|
|
 |
|
|
 |
|
 |
Goran, don't be a fool - C++ HAS a XOR operator. IT IS: ^
Try this
int i=6, j=5;
int k = i^j;
cout << k; //WHAT IS THAT? A XOR OPERATION!!!
|
|
|
|
 |
|
 |
That's bitwise, not logical, ya monkey. Go take a CS class.
|
|
|
|
 |
|
 |
C'mon monkey, can't you do this:
function logXOR(int i, int j)
|
|
|
|
 |
|
 |
C'mon monkey, can't you do this:
bool logXOR(int i, int j)
{ return ((i^j)==0); }
//isn't that the BOOL operator performing JUST FINE
//as a LOGICAL OPERATOR...a little bit of
//imagination please!
|
|
|
|
 |
|
 |
oops I meant to say BITWISE XOR operator performing as LOGICAL OPERATOR...need more coffee!
|
|
|
|
 |
|
 |
bool logXOR(int i, int j)
{ return ((i^j)==0); }
Let's check it out. You have a bitfield f=0b01010 and you want to test for the presence of either the second bit or the fourth, not both, and at least one of them. Well you want to perform a logical XOR : f&a ^^ f&b with a=0b01000 and b=0b00010
#include
using namespace std;
bool logXOR(int i, int j)
{
return ((i^j)==0);
}
bool anotherLogXOR(int i, int j)
{
return (bool)i ^ (bool)j;
}
int main()
{
// a = 0b01000
// b = 0b00010
// field = { 0b00000, 0b01000, 0b00010, 0b01010 };
int a = 8, b = 2;
int fields[] = { 0, 2, 8, 10 };
bool actual_results[] = { false, true, true, false };
for (int i=0;i<4;i++)
{
cout << fields[i] << "&" << a << " ^^ " << fields[i] << "&" << b;
cout << " = " << actual_results[i] << " while logXOR() says ";
cout << logXOR(fields[i]&a, fields[i]&b) << " (";
if (anotherLogXOR(fields[i]&a,fields[i]&b)==actual_results[i])
cout << "OK";
else
cout << "WRONG";
cout << ")" << endl;
}
}
Test your stuff before you submit !
|
|
|
|
 |
|
 |
... there's no easy way to create an "operator^^" to make it look better.
|
|
|
|
 |
|
 |
your could simple do :
#define LOGXOR(l,r) (bool)(l) ^ (bool)(r)
|
|
|
|
 |
|
 |
Of course. However, call me silly, but I prefer writing operators in infix notation. - Goran.
|
|
|
|
 |
|
 |
but it seems that there's a lot more overhead using your method... you do the type casting (which is all you really need to do this) PLUS having a new instance of a structure created EVERY time you want to XOR.
just seems ridiculous to me...
BUT the code is very nice if you had a more complex operation such as byte by byte compression or such... then it wouldn't be possible to do only type-casting
- Roman -
|
|
|
|
 |
|
 |
Optimizing compilers should be able to eliminate all unnecessary overhead. I don't really know (nor I'm interested) whether that's the case with MSVC. - Goran.
|
|
|
|
 |
|
 |
OK, I lied; I *was* interested... As expected, on MSVC7, there is no additional overhead at all. For example, constant expressions like 5 log_xor 2 are fully calculated at compile-time. - Goran.
|
|
|
|
 |