44

My question uses Java as an example, but I guess it applies to probably all.

Is there any practical difference between the XOR operator (^ in Java) and the not-equal-to operator (!= in Java), when comparing booleans?

I evaluated things here, but I just kept wondering (seems weird, two things equal)... and didn't find anything on the net. Just one discussion in some forum that ended quickly without any result.

Alexander
  • 59,041
  • 12
  • 98
  • 151
davidcesarino
  • 16,160
  • 16
  • 68
  • 109
  • Some related discussion in this question (I personally wouldn't consider it a duplicate, per say): http://stackoverflow.com/questions/160697/is-it-good-practice-to-use-the-xor-operator-in-java-for-boolean-checks – eldarerathis Nov 14 '10 at 00:30
  • A tangential note: As the answers below have said, the two operators mean the same thing for booleans. This is not true in the general case, at least in C++. Since operators can be overloaded in C++, the operators `!=` and `^` may or may not do the same thing. Besides, using `!=` as opposed to `^` makes it rather clear that a test of inequality is being performed. – In silico Nov 14 '10 at 01:30
  • I'm sorry, I didn't even see that question, maybe because the titles are different and the question verifier didn't catch it. Anyway, the answers here were much much better and enlightened, in my humble opinion. About what's said there... I tend to agree: if you're about to save one of a few letters that leaves the code more obscure, I prefer going verbose. I will stick with != in these situations. – davidcesarino Nov 14 '10 at 01:44
  • In silico, that's why I said "java" just to be sure. I was afraid there could be differences compared to other languages. – davidcesarino Nov 14 '10 at 01:45
  • 1
    [related](https://stackoverflow.com/questions/55598277/is-there-a-useful-difference-between-p-q-and-p-q-for-booleans/55601399#55601399) – Eugene May 03 '19 at 08:53

6 Answers6

46

For Boolean values, they mean the same thing - although there's a compound assignment operator for XOR:

x ^= y;

There's no equivalent compound assignment operator for inequality.

As for why they're both available - it would be odd for XOR not to be available just because it works the same way as inequality. It should logically be there, so it is. For non-Boolean types the result is different because it's a different result type, but that doesn't mean it would make sense to remove XOR for boolean.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Yes, those were my thoughts exactly! I just wanted to make sure because generally speaking I don't see many things that are essentially the same serving one same purpose (an Occam's razor of sorts I guess...). This concern obviously does not apply to the ternary, which is, of course, a better way and cleaner way to do the same thing. But with my case here it's really just the same. But yes, you were right on the point of the issue and my question. Thank you. – davidcesarino Nov 14 '10 at 01:32
29

As stated in the Java Language Specification:

The result of != is false if the operands are both true or both false; otherwise, the result is true. Thus != behaves the same as ^ (§15.22.2) when applied to boolean operands.

In addition if you try looking at bytecode of a simple snippet:

void test(boolean b1, boolean b2) {
    boolean res1 = b1^b2;
    boolean res2 = b1!=b2;
}

you obtain:

test(ZZ)V
   L0
    LINENUMBER 45 L0
    ILOAD 1
    ILOAD 2
    IXOR
    ISTORE 3
   L1
    LINENUMBER 46 L1
    ILOAD 1
    ILOAD 2
    IXOR
    ISTORE 4
   L2
    LINENUMBER 47 L2
    RETURN
   L3

This assures that, in addition to the same semantics, there's no any actual practical difference in implementation. (you can also see that internally ints are used to store boolean values)

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
Jack
  • 131,802
  • 30
  • 241
  • 343
  • Thank you very much for that deep explanation of how it works! – davidcesarino Nov 14 '10 at 01:36
  • @Jack providing byte-code for this simple methods is useless, `JIT` [does the really beautiful things](https://stackoverflow.com/questions/55598277/is-there-a-useful-difference-between-p-q-and-p-q-for-booleans/55601399#55601399) – Eugene May 03 '19 at 08:52
  • @Eugene: we're dealing with semantics, not performance. JIT does beautiful things but it's required to keep the semantics of the original bytecode. So analyzing performance difference is a nice addition but doesn't make bytecode less useful. Actually I could argue that microbenchmarks are useless at the same time :) – Jack Jul 20 '20 at 17:31
5

Yes, you can use XOR to test booleans for (in)equality, although the code is less intuitive: if (x ^ y) versus if (x != y).

LukeH
  • 263,068
  • 57
  • 365
  • 409
3

With boolean values, there should be no difference. You should choose whichever is more appropriate for your sense of operation.

Example:

bool oldChoice = ...;
bool newChoice = ...;
if (oldChoice != newChoice)
    ...

Here XOR would give the same result, but will not reflect the real code intention.

Vlad
  • 35,022
  • 6
  • 77
  • 199
0

They should be essentially the same in this case.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

There's a big difference, XOR works at bit-level, keeping differences as ones, so 0b0011 xor 0b1101 => 0b1110

regards, //t

Teson
  • 6,644
  • 8
  • 46
  • 69