Is there an XNOR (Logical biconditional) operator in C#?
Asked Answered
A

5

201

I could not find an 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)?

Allogamy answered 14/8, 2011 at 0:14 Comment(7)
This operator is more commonly known as == for boolean operands...Glennglenna
@Magnus Hoff : nice very nice point!Involve
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 ;)Sheer
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...Chondroma
sorry, Kerrek, I'm not from that crowd. And spender is quite right here -)Allogamy
Just fell into the same hole twice. @spender's metaphor is spot onSchoolmaster
Maybe he's a mathematician who recently delved into programming.Nannienanning
S
334

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

Seismism answered 14/8, 2011 at 0:22 Comment(2)
In C, ! operator can be used to convert int's to "well-behaved" booleans: !a==!b.Sulfonal
@Sulfonal And !! (that's two logical "not" operators) normalizes any scalar value to 0 or 1.Seismism
L
5

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

Lalittah answered 28/1, 2012 at 20:48 Comment(0)
F
4

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

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

Funiculate answered 14/8, 2011 at 0:16 Comment(4)
This is bitwise, not a logicalInvolve
I think the poster knows that as he's included it in his question.Funiculate
@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.aspxAllogamy
@trailmax: cool stuff, thanks for pointing on this! Really devil is in detail!Involve
V
-1

I there is a few bitwise operations I don't see as conventional in the whole discussion. Even in c, appending or inserting to the end of a string, all familiar in standard IO and math libs. this is where I believe the problem lies with not and Xnor, not familiar with python but I propose the following example

function BitwiseNor(a as integer)
    l as string
    l=str(a)
    s as string
    For i=len(l) to 0
        s=(str(i)+"="+Bin(i)) //using a string in this example because binary or base 2 numnbers dont exists in language I have used
    next i
endfunction s

function BitwiseXNor(a as integer,b as integer)
    r as integer
    d as integer
    c as string
    c=str(BitwiseOr(a,b))
    r=(val(c,2)) //the number to in this conversion is the base number value
endfunction r 
Vocalic answered 27/6, 2022 at 18:49 Comment(0)
O
-11

You can use === operator for XNOR. Just you need to convert a and b to bool.

if (!!a === !!b) {...}
Oeo answered 31/3, 2017 at 11:6 Comment(4)
only C# does not have === operatorAllogamy
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# eitherCoridon
as already stated, c# does not have triple equal sign Operator.Eogene
=== is not a operatr in C#...(===) is used is JavaScript.Hayward

© 2022 - 2024 — McMap. All rights reserved.