195

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

starball
  • 20,030
  • 7
  • 43
  • 238
trailmax
  • 34,305
  • 22
  • 140
  • 234

5 Answers5

325

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

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
6

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

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
Braden Best
  • 8,830
  • 3
  • 31
  • 43
4

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

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

Griffin
  • 13,184
  • 4
  • 29
  • 43
  • 2
    This is bitwise, not a logical – sll Aug 14 '11 at 00:20
  • I think the poster knows that as he's included it in his question. – Griffin Aug 14 '11 at 00:21
  • 4
    @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 http://msdn.microsoft.com/en-us/library/zkacc7k1.aspx – trailmax Aug 14 '11 at 00:44
  • @trailmax: cool stuff, thanks for pointing on this! Really devil is in detail! – sll Aug 14 '11 at 00:47
0

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 
phuzi
  • 12,078
  • 3
  • 26
  • 50
Peter
  • 1
-10

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

if (!!a === !!b) {...}