What is the point of logical operators in C?

I'm just wondering if there is a logical XOR operator in C (something like && for AND, but for XOR). I know that I can split XOR into AND, NOT and OR, but a simple XOR would be much better. Then it occurred to me that if I use the usual bitwise XOR operator between two conditions, it might just work. And for my tests this was done.

Consider:

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

Just for this rather stupid example, if I need k to be more or more than j, but not both, XOR would be very convenient.

 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". Bringing the results of two comparisons into two integers led to the fact that 2 integers contained 1, so XOR produced a false. Then I tried this with and and | bitwise operators and both gave the expected results. All this makes sense, knowing that true conditions have non-zero values, while false conditions have zero values.

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

+5
c bitwise-operators logical-operators xor
source share
5 answers

You don’t need logical XOR, I forgot the SO question, but it looks like what you think, basically we don’t need XOR, this is equivalent ! = 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 will search for my favorites and embed the link later ...

+13
source share

Bitwise operators do not work "exactly the same" as && and || operator. For starters, && and || perform a short-circuited estimate, while bitwise operators do not. In other words, you cannot do something like this with bitwise operators:

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

because if you said:

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

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

+11
source share

Bitwise XOR does not work the same as logical XOR when its operands are integer values:

 2^4 ? "Valid" : "Invalid" 

gives "Valid", but must indicate "Invalid"

+1
source share

In C arguments, logical operators are treated as logical values: any zero is considered “false”, and everything else (yes, negative values ​​too) are “true”. Bitwise operators work with individual bits, and, as Neil has already noted, they do not undergo short circuit evaluation as logical operations.

In your example, the results are fully valid and expected, since the bit-wise xor between the two units is zero.

+1
source share

If you need the logical xor operator in C, you can use this:

 #define xor != 0 ^ !! 

It works by converting both sides of an expression into logical ones and xoring them. You can use it as if you were using && or ||, for example:

 if (a xor b) 

AFAICT, there are no problems with it.

+1
source share

All Articles