What's the difference between XOR and NOT-EQUAL-TO?

前端 未结 6 2080
北恋
北恋 2020-12-15 17:11

My question uses Java as an example, but I guess it applies to probably all.

Is there any practical difference between the XOR operator (^ in Java) and

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 17:20

    With boolean values, there should be no difference. You should choose whichever is more appropriate for your sense of operation.

    Example:

    bool oldChoice = ...;
    bool newChoice = ...;
    if (oldChoice != newChoice)
        ...
    

    Here XOR would give the same result, but will not reflect the real code intention.

提交回复
热议问题