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

前端 未结 7 1925
孤街浪徒
孤街浪徒 2020-12-16 13:31

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

I understand that it wouldn\'t

7条回答
  •  天涯浪人
    2020-12-16 14:05

    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.

提交回复
热议问题