1

The title is pretty straightforward, I was just wondering if there was a difference (of any kind, performance or anything that comes to mind) between stating :

if (!(x == y)) {

}

and

if (x != y) {

}

Also, x & y are something primitives or simple objects like int, String, double ,etc. Not custom objects. But if it makes a huge difference I'd be glad to hear about it !

Considering they both check for equality, but one then uses the "opposite" afterwards, I'm not sure if it's different.

Even though this difference would be extremely minor, it arouses my curiousity !

EDIT :

The reason I thought of that (so you see exactly what I mean) was while typing this :

if (!(myTextField.length == 5))

instead of

if (mytextfield.length != 5)

So you see exactly how I meant it. String length.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Gil Sand
  • 5,802
  • 5
  • 36
  • 78
  • 1
    Depending on operator specificity there might be quite a difference. – Sirko Jan 08 '15 at 13:01
  • Really? Can you elaborate (maybe in an answer so i can give you points for it). I was thinking about int, string, just regular stuff. Not custom objects. – Gil Sand Jan 08 '15 at 13:02
  • 3
    I assume you mean `(! (x == y))` instead of `(! x == y)`. If so, could you make this explicit in the question? – tobias_k Jan 08 '15 at 13:04
  • 1
    `!x == y` means "(not x) equal to (y)" and `!(x == y)` means "not (x and y equal)". – h2ooooooo Jan 08 '15 at 13:05
  • 1
    @h2ooooooo I spelt it wrong, see my latest edit – Gil Sand Jan 08 '15 at 13:11
  • @Zil, what does `!myTextField.length` mean? Or did you forget the `(...)` in that line? – aioobe Jan 08 '15 at 13:14
  • @Zil Your edits to the post do not clarify at all, right now you ask about `!(x == y)` and argue that you asked that starting from `!x == y`, which is completely different. It's either one or the other. – Riccardo T. Jan 08 '15 at 13:14
  • I also changed the main question a little after, i meant !(x==y), and (even though i wrote it) never meant !x == y. I had forgotten parentheses. Sorry about all that ! @aioobe It's just a String length, so an int. – Gil Sand Jan 08 '15 at 13:18
  • It should finally be all fine now ! haha, damn, took me a while to even write down my question properly – Gil Sand Jan 08 '15 at 13:19
  • @Zil What language is this? Javascript? AS3? – h2ooooooo Jan 08 '15 at 13:20
  • Its none, i was writing in objective-C but i "simplified" it in pseudo-code. I removed the brackets and all the useless bits of code around, and of course put it wrong in here. Here is the original post where i asked myself this. http://stackoverflow.com/a/27840683/3603502 – Gil Sand Jan 08 '15 at 13:21
  • What language? It'll matter. – Lightness Races in Orbit Jan 08 '15 at 13:26
  • `! (x==y)` could be a tiny bit slower, because technically it are two operations. Of course most optimizer would catch this and merge to the better operation, and `!`ing a boolean is one of the fastest operations possible anyway – cypherabe Jan 08 '15 at 13:26
  • 1
    @LightnessRacesinOrbit Objective-C then – Gil Sand Jan 08 '15 at 13:27

5 Answers5

5

!(x == y) and x != y are logically equivalent and I doubt any language, compiled or interpreted, would evaluate them differently internally. In other words, I'd be surprised if you found a measurable difference.

Some languages such as C++ and Scala lets you override these operators, in which case it's a different story of course.

aioobe
  • 413,195
  • 112
  • 811
  • 826
2

Usually the ! operator is preceding the == or != operator.

Hence, this

!x == y

is the same as this

(!x) == y

So you negate x, before comparing it to y. This is obviously not the same as checking just for inequality between x and y:

x != y

To make sure the equality check is done before negating, you should use another pair of parenthesis:

!(x == y)
Sirko
  • 72,589
  • 19
  • 149
  • 183
  • You're assuming C (*et al*) operator precedence but the question is language-agnostic. – Paul R Jan 08 '15 at 13:06
  • @PaulR I start out with "usually", which confines this answer to languages, where this holds true. If this assumption does not hold for a specific language, the rest of the answer becomes obsolete. – Sirko Jan 08 '15 at 13:07
  • 1
    I edited the question at the bottom to be clearer, I typed it poorly in my main example – Gil Sand Jan 08 '15 at 13:08
  • @Zil Looks like JavaScript code to me. The above explanation above still holds true. First `myTextField.length` is negated and thereby converted to a `boolean` and afterwards the comparison is evaluated, which in turn involved some type conversions. – Sirko Jan 08 '15 at 13:14
1

First case is less readable. After optimization it is equivalent to second.

i486
  • 6,491
  • 4
  • 24
  • 41
0

These are not the same. But if you enclose the first one as if(!(x == y)) then these are same.

But in case if you write it like (X != Y) and (! X == Y),

First is simple that X is not equal to Y.

But in second (!X == Y),,, at first !X will be evaluated before comparing to Y, Lets say if X is of Boolean type then it will invert the X value (True to False and False to True) and then will compare it to Y

Amer Zafar
  • 419
  • 2
  • 11
  • You're assuming C (*et al*) operator precedence but the question is language-agnostic. – Paul R Jan 08 '15 at 13:07
  • Yes, but precedence will be override in any language if parenthesis are used properly. – Amer Zafar Jan 08 '15 at 13:09
  • 2
    My question was poorly written, i didnt' mean " the opposite of X ", i meant " the opposite of the result of the equality ", i forgot the parentheses. Knowing this, is there a difference between both then? Performance maybe? – Gil Sand Jan 08 '15 at 13:09
-2

!x == y means if ( ( x == 0 && y == 1 ) || ( x != 0 && y == 0 ) ). In other words, it is different than x!=y.

i486
  • 6,491
  • 4
  • 24
  • 41
  • You're assuming C (*et al*) operator precedence but the question is language-agnostic. – Paul R Jan 08 '15 at 13:06
  • If the priority is not like `C` then there is no difference (with a good optimizer). Same question like "What is the difference between `x != y` and `y != x`". – i486 Jan 08 '15 at 13:08
  • This is simply not true in many languages. For non-boolean but built-in `y`, conversion to boolean means `y > 1`, not `y == 1` ... and your thing about `x == 0 && y == 1` is just obviously nonsense. – Lightness Races in Orbit Jan 08 '15 at 13:27
  • Sorry, but it seems this is `C`. See new tags - Objective-C. It seems my assumption is right. – i486 Jan 08 '15 at 13:30
  • @Lightness Races in Orbit - what is nonsense with `x==0 && y == 1`, because I cannot understand your thought? In case when x is 0, then !x will be 1 and the comparison is `y == 1` - correct or not? – i486 Jan 08 '15 at 13:32
  • @LightnessRacesinOrbit In C, all non-zero boolean expression values evaluate to `true`. – jlehr Jan 08 '15 at 14:41
  • @jlehr: Yes, That's what I said (although I meant `y >= 1` when I wrote `y > 1`) – Lightness Races in Orbit Jan 08 '15 at 14:48
  • @LightnessRacesinOrbit Which is still incorrect, because it doesn't account for negative values of `y`. – jlehr Jan 08 '15 at 14:49
  • @jlehr: True. The fact remains, the logic expressed in this answer is at best true in one or two languages, certainly inexpressive in those languages, and at worst downright misleading. – Lightness Races in Orbit Jan 08 '15 at 15:39