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

trailmax picture trailmax · Aug 14, 2011 · Viewed 54.3k times · Source

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)?

Answer

Keith Thompson picture Keith Thompson · Aug 14, 2011

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 !.)