tags:

views:

289

answers:

4

I am trying to find the most effective way of writing a XNOR gate in C.

if(VAL1 XNOR VAL2)
{
    BLOCK;
}

Any suggestions?

Thanks.

+1  A: 
if(!(val1^val2))
{
    block;
}

edit: outside of logical operations, you'd probably want ~(val1^val2) to be exact, but i find the ! clearer.

KillianDS
A: 

Presuming val1 and val2 are to be treated in the normal C logical boolean fashion (non-zero is true), then:

if (!val1 ^ !!val2)
{

}

will do the trick.

caf
+6  A: 

With two operands this is quite simple:

if (val1 == val2)
{
    block;
}
Tomas Markauskas
A: 

What do you mean by "most effective"?

Tomas' answer assumes that VAL1 and VAL2 are 0 and 1, rather than the C definition of logical true and false as "0 is false, non-zero is true". If your values are guaranteed to be 0 and 1, it's a great answer.

If you expect the condition to work in a C-ish manner (0 = false, non-0 = true), then I think KillianDS answer is fine.

virtualsue