Reputation: 111
I am trying to find the most effective way of writing a XNOR gate in C.
if(VAL1 XNOR VAL2)
{
BLOCK;
}
Any suggestions?
Thanks.
Upvotes: 11
Views: 13183
Reputation: 11596
With two operands this is quite simple:
if (val1 == val2)
{
block;
}
Upvotes: 27
Reputation: 239011
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.
Upvotes: 0
Reputation: 17176
if(!(val1^val2))
{
block;
}
edit: outside of logical operations, you'd probably want ~(val1^val2)
to be exact, but i find the ! clearer.
Upvotes: 7