Is there XNOR (Logical biconditional) operator in C#?

前端 未结 4 603
自闭症患者
自闭症患者 2020-12-23 09:03

I\'m new to C# and could not find XNOR operator to provide this truth table:

a  b    a XNOR b
----------------
T  T       T
T  F       F
F  T       F
F  F       T         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-23 09:11

    XOR = A or B, but Not A & B or neither (Can't be equal [!=])
    XNOR is therefore the exact oppoiste, and can be easily represented by == or ===.

    However, non-boolean cases present problems, like in this example:

    a = 5
    b = 1
    
    if (a == b){
    ...
    }
    

    instead, use this:

    a = 5
    b = 1
    
    if((a && b) || (!a && !b)){
    ...
    }
    

    or

    if(!(a || b) && (a && b)){
    ...
    }
    

    the first example will return false (5 != 1), but the second will return true (a[value?] and b[value?]'s values return the same boolean, true (value = not 0/there is a value)

    the alt example is just the reversed (a || b) && !(a && b) (XOR) gate

提交回复
热议问题