Logical XOR operator in C++?

后端 未结 11 1239
轮回少年
轮回少年 2020-11-29 15:54

Is there such a thing? It is the first time I encountered a practical need for it, but I don\'t see one listed in Stroustrup. I intend to write:

// Detect wh         


        
11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 16:08

    (A || B) && !(A && B)

    The first part is A OR B, which is the Inclusive OR; the second part is, NOT A AND B. Together you get A or B, but not both A and B.

    This will provide the XOR proved in the truth table below.

    |-----|-----|-----------|
    |  A  |  B  |  A XOR B  |
    |-----|-----|-----------|
    |  T  |  T  |   False   |
    |-----|-----|-----------|
    |  T  |  F  |   True    |
    |-----|-----|-----------|
    |  F  |  T  |   True    |
    |-----|-----|-----------|
    |  F  |  F  |   False   |
    |-----|-----|-----------|
    

提交回复
热议问题