views:

271

answers:

6

I was just wondering if there is an XOR logical operator in C (something like && for AND but for XOR). I know I can split an XOR into ANDs, NOTs and ORs but a simple XOR would be much better. Then it occurred to me that if I use the normal XOR bitwise operator between two conditions, it might just work. And for my tests it did.

Consider:

int i = 3;
int j = 7;
int k = 8;

Just for the sake of this rather stupid example, if I need k to be either greater than i or greater than j but not both, XOR would be quite handy.

if ((k > i) XOR (k > j))
   printf("Valid");
else
   printf("Invalid");

or

printf("%s",((k > i) XOR (k > j)) ? "Valid" : "Invalid");

I put the bitwise XOR ^ and it produced "Invalid". Putting the results of the two comparisons in two integers resulted in the 2 integers to contain a 1, hence the XOR produced a false. I've then tried it with the & and | bitwise operators and both gave the expected results. All this makes sense knowing that true conditions have a non zero value, whilst false conditions have zero values.

I was wondering, is there a reason to use the logical && and || when the bitwise operators &, | and ^ work just the same?

Thanks Reuben

+11  A: 

The bitwise operators do not work "just the same" as the && and || operator. For a start, && and || perform short-circuited evaluation, whereas the the bitwise operators do not. In other words, you can't do something like this with the bitwise operators:

int * p = 0;
(p != 0) && (*p = 1);

because if you said:

(p != 0) & (*p = 1);

both subexpressions would be evaluated, and you would dereference the null pointer.

anon
Internally no, but I still got the same results
reubensammut
@reubensammut: You will not get the same results if you depend on the short-circuitedness. `if(returnsTrue() || explodesWorld())` will not destroy the world, whereas `if(returnsTrue() | explodesWorld())` will.
unwind
+13  A: 

You don't need logical XOR, I have forgotten the SO question, but it's similar to what you're thinking, basically we don't need XOR, it's equivalent to != anyway

FALSE XOR FALSE == FALSE
FALSE XOR TRUE == TRUE
TRUE XOR FALSE == TRUE
TRUE XOR TRUE == FALSE


FALSE != FALSE == FALSE
FALSE != TRUE == TRUE
TRUE != FALSE == TRUE
TRUE != TRUE == FALSE

I'll search my favorites, and paste here the link later...

Michael Buen
You're right, != is the same. Thanks
reubensammut
mipadi
+1  A: 

Bitwise XOR does not work as would a logical XOR when its operands are integer values:

2^4 ? "Valid" : "Invalid"

gives "Valid" but should give "Invalid"

mouviciel
I meant XOR when used for conditions
reubensammut
In C, 2 is a valid way for expressing TRUE. Many people use it for checking a NULL pointer: `if (!p) ...`
mouviciel
+1  A: 

In C arguments of logical operators are treated as Boolean values - anything zero is treated as "false", and anything else (yes, negative values too) are "true". Bitwise ops work on individual bits, and as Neil already noted, are not subject to short-circuit evaluation as logical ops are.

In your example the results are totally valid and expected since bit-wise xor between two ones is zero.

Nikolai N Fetissov
+2  A: 

Furthermore, there is another difference between && || and & | besides the short circuit evaluation Neil Butterworth mentioned and the binary/logical value difference Nikolai N Fetissov mentioned. The order of precedence is different.

a != 0 & b != 0 will not give the same result as a != 0 && b != 0. The first one is equivalent to (a != (0 & b)) != 0) while the second one is equivalent to (a != 0) && (b != 0).

tristopia
A: 

If you want a logical xor operator in C, then you can use this:

#define xor != 0 ^ !!

It works by converting both sides of the expression to booleans and xoring them. You can use it as if you were using && or ||, like this:

if (a xor b)

AFAICT, there aren't any problems with it.

Joe D