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

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

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

I understand that it wouldn\'t

相关标签:
7条回答
  • 2020-12-16 14:27

    For non-bool operands, I guess what you would want is for a ^^ b to be evaluated as:

    (a != 0) ^ (b != 0)
    

    Well, you have the above option and you have a few options listed in other answers.

    The operator ^^ would be redundant for bool operands. Talking only about boolean operands, for the sake of argument, let's pretend that ^ was bitwise-only and that ^^ existed as a logical XOR. You then have these choices:

    • & - Bitwise AND -- always evaluates both operands
    • && - Logical AND -- does not always evaluate both operands
    • | - Bitwise OR -- always evaluates both operands
    • || - Logical OR -- does not always evaluate both operands
    • ^ - Bitwise XOR -- must always evaluate both operands
    • ^^ - Logical XOR -- must always evaluate both operands

    Why didn't they create ^^ to essentially convert numerical values into bools and then act as ^? That's a good question. Perhaps because it's more potentially confusing than && and ||, perhaps because you can easily construct the equivalent of ^^ with other operators.

    0 讨论(0)
提交回复
热议问题