Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no ^^ operator in C/C++?

& has &&. | has ||. Why doesn't ^ have ^^?

I understand that it wouldn't be short-circuiting, but it would have different semantics. In C, true is really any non-zero value. Bitwise XOR is not always the same thing as logical XOR:

int a=strcmp(str1,str2);// evaluates to 1, which is "true"
int b=strcmp(str1,str3);// evaluates to 2, which is also "true"
int c=a ^^ b; // this would be false, since true ^ true = false
int d=a ^ b; //oops, this is true again, it is 3 (^ is bitwise)

Since you can't always rely on a true value being 1 or -1, wouldn't a ^^ operator be very helpful? I often have to do strange things like this:

if(!!a ^ !!b) // looks strange
like image 476
Zifre Avatar asked May 07 '09 22:05

Zifre


People also ask

Why is not operator used?

The NOT operator is used in most programming languages which support logical and comparison operators. In the programming world, it is mainly used to control the flow of the program. It is used in construction of logical statements and in supporting bitwise negation.

What is the operator in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

How does the logical NOT operator work in C?

Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.

Can you use || in C?

The result of a logical OR ( || operator in C) is true if EITHER of its inputs is true. Similarly logical AND ( && operator in C) is true if BOTH of its inputs are true.


2 Answers

Dennis Ritchie answers

There are both historical and practical reasons why there is no ^^ operator.

The practical is: there's not much use for the operator. The main point of && and || is to take advantage of their short-circuit evaluation not only for efficiency reasons, but more often for expressiveness and correctness.
[...]
By contrast, an ^^ operator would always force evaluation of both arms of the expression, so there's no efficiency gain. Furthermore, situations in which ^^ is really called for are pretty rare, though examples can be created. These situations get rarer and stranger as you stack up the operator--

if (cond1() ^^ cond2() ^^ cond3() ^^ ...) ...

does the consequent exactly when an odd number of the condx()s are true. By contrast, the && and || analogs remain fairly plausible and useful.

like image 133
Don Avatar answered Oct 20 '22 01:10

Don


Technically, one already exists:

a != b

since this will evaluate to true if the truth value of the operands differ.

Edit:

Volte's comment:

(!a) != (!b)

is correct because my answer above does not work for int types. I will delete mine if he adds his answer.

Edit again:

Maybe I'm forgetting something from C++, but the more I think about this, the more I wonder why you would ever write if (1 ^ 2) in the first place. The purpose for ^ is to exclusive-or two numbers together (which evaluates to another number), not convert them to boolean values and compare their truth values.

This seems like it would be an odd assumption for a language designer to make.

like image 44
John Rasch Avatar answered Oct 19 '22 23:10

John Rasch