Is there XNOR (Logical biconditional) operator in C#?
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 c#, 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.
Related videos on Youtube
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, 2022Comments
-
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 almost 13 yearsThis operator is more commonly known as
==
for boolean operands... -
sll almost 13 years@Magnus Hoff : nice very nice point!
-
spender almost 13 yearsI 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 almost 13 yearsMaybe 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 almost 13 yearssorry, Kerrek, I'm not from that crowd. And spender is quite right here -)
-
enzi about 5 yearsJust fell into the same hole twice. @spender's metaphor is spot on
-
-
sll almost 13 yearsThis is bitwise, not a logical
-
Griffin almost 13 yearsI think the poster knows that as he's included it in his question.
-
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 almost 13 years@trailmax: cool stuff, thanks for pointing on this! Really devil is in detail!
-
ivan_pozdeev over 9 yearsIn C,
!
operator can be used to convertint
's to "well-behaved" booleans:!a==!b
. -
trailmax about 7 yearsonly C# does not have
===
operator -
Cookie over 6 yearsnone 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 over 6 yearsas already stated, c# does not have triple equal sign Operator.
-
Keith Thompson over 4 years@ivan_pozdeev And
!!
(that's two logical "not" operators) normalizes any scalar value to0
or1
. -
Darkcoder over 4 years=== is not a operatr in C#...(===) is used is JavaScript.