xor with 3 values

后端 未结 10 1510
野性不改
野性不改 2020-12-30 22:12

I need to do an xor conditional between 3 values, ie i need one of the three values to be true but not more than one and not none.

I thought i could use the xor ^ op

10条回答
  •  悲&欢浪女
    2020-12-30 22:20

    XOR is a binary operator and does not function on more than two operands. Given the order of operations, when you are looking at: (false ^ false ^ false) you are really getting `((false ^ false) ^ false).

    Bearing that in mindm, in terms of evaluating whether this is going to work for you, it may be useful to build yourself a truth table for your desired operation. In the case of the example listed above you are looking at:

    (Op1 XOR Op2) XOR Op3 = Result    
     F       F        F     F
     F       F        T     T 
     F       T        F     T 
     F       T        T     F
     T       F        F     T
     T       F        T     F
     T       T        F     F
     T       T        T     T
    

    Which makes XOR a problem in the case where all operands are true and may require some special handling say by adding an AND not (Op1 AND Op2 AND Op3).

提交回复
热议问题