Is it good practice to use the XOR (^) operator in Java for boolean checks?
Posted
by Pete
on Stack Overflow
See other posts from Stack Overflow
or by Pete
Published on 2008-10-02T02:58:12Z
Indexed on
2010/04/10
8:43 UTC
Read the original article
Hit count: 232
I personally like the 'exclusive or' operator when it makes sense in context of boolean checks because of its conciseness. I much prefer to write
if (boolean1 ^ boolean2)
{
//do it
}
than
if((boolean1 && !boolean2) || (boolean2 && !boolean1))
{
//do it
}
but I often get confused looks (from other experienced java developers, not just the newbies), and sometimes comments about how it should only be used for bitwise operations. I'm curious as to the best practices others use around the '^' operator.
© Stack Overflow or respective owner