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

64,906

Solution 1

XNOR is simply equality on booleans; use A == B.

This is an easy thing to miss, since equality isn't commonly applied to booleans. And there are languages where it won't necessarily work. For example, in C, any non-zero scalar value is treated as true, so two "true" values can be unequal. But the question was tagged , which has, shall we say, well-behaved booleans.

Note also that this doesn't generalize to bitwise operations, where you want 0x1234 XNOR 0x5678 == 0xFFFFBBB3 (assuming 32 bits). For that, you need to build up from other operations, like ~(A^B). (Note: ~, not !.)

Solution 2

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

Solution 3

No, You need to use !(A^B)

Though I suppose you could use operator overloading to make your own XNOR.

Share:
64,906

Related videos on Youtube

trailmax
Author by

trailmax

tech.trailmax.info - My blog. I write about C# and ASP.Net MVC (and sometimes about other random stuff) hrnet.trailmax.info My other blog. There I write about a technology I have been exposed for longer than I wanted. And now I know too much about HR.Net. This is part of my consulting service.

Updated on July 08, 2022

Comments

  • trailmax
    trailmax almost 2 years

    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
    

    Is there a specific operator for this? Or I need to use !(A^B)?

    • mrivard
      mrivard almost 13 years
      This operator is more commonly known as == for boolean operands...
    • sll
      sll almost 13 years
      @Magnus Hoff : nice very nice point!
    • spender
      spender almost 13 years
      I think the phrase "can't see the wood for the trees" is highly appropriate here. Voting up because we've all been here once or twice ;)
    • Kerrek SB
      Kerrek SB almost 13 years
      Maybe the OP iz l33t k!d who wants to write awesome shellcodez and needs to somehow hide the comparison operation. It's a possibility...
    • trailmax
      trailmax almost 13 years
      sorry, Kerrek, I'm not from that crowd. And spender is quite right here -)
    • enzi
      enzi about 5 years
      Just fell into the same hole twice. @spender's metaphor is spot on
  • sll
    sll almost 13 years
    This is bitwise, not a logical
  • Griffin
    Griffin almost 13 years
    I think the poster knows that as he's included it in his question.
  • trailmax
    trailmax almost 13 years
    @sllev you almost got me, I had to double check it. In C# ^ is logical if operated on boolean. If operated on integral types, it is bitwise. Please see msdn.microsoft.com/en-us/library/zkacc7k1.aspx
  • sll
    sll almost 13 years
    @trailmax: cool stuff, thanks for pointing on this! Really devil is in detail!
  • ivan_pozdeev
    ivan_pozdeev over 9 years
    In C, ! operator can be used to convert int's to "well-behaved" booleans: !a==!b.
  • trailmax
    trailmax about 7 years
    only C# does not have === operator
  • Cookie
    Cookie over 6 years
    none of this answer is correct, === the non-coercive operator is javascript and the double !! before a value in an evaluation is not valid in c# either
  • cramopy
    cramopy over 6 years
    as already stated, c# does not have triple equal sign Operator.
  • Keith Thompson
    Keith Thompson over 4 years
    @ivan_pozdeev And !! (that's two logical "not" operators) normalizes any scalar value to 0 or 1.
  • Darkcoder
    Darkcoder over 4 years
    === is not a operatr in C#...(===) is used is JavaScript.