@
@
@xpostman2010-09-25 20:17:15
JavaScript
@xpostman, 2010-09-25 20:17:15

Unusual use of bitwise XOR in Javascript?

Hello.
While recently debugging another piece of someone else's code, I came across such a cycle:
while (--i ^ -1) /* тело цикла */;
at the same time, initially i is equal to some positive number. That is, the bitwise operator is used here for integers. After a little thought, I realized that the result of a bitwise XOR will turn to zero only in the case when the numbers are equal. Then the condition is cast to true . Otherwise, it will be non-zero, which is cast to false .
It turns out that we can use bitwise XOR instead of the != operator (if we know in advance that the compared values ​​are numbers).
Hence, I have a question that I want to put to the audience.
Why does the author use XOR?
I was thinking about this all evening yesterday, and the most plausible reason seemed to me to be the desire to write "cool obscure code." I immediately remembered how, when I started to comprehend the basics of programming, I suddenly discovered that I could write "concise code that is difficult to understand and that works." It seemed to me that this indicates some kind of skill. In fact, it is clear that this indicates the opposite. In favor of this hypothesis was also the fact that the rest of the code in the vicinity of the given fragment was written in a similar way.
However, there was one more thought that didn't bother me. I imagined that if I wanted to complicate the code, for some reason it would not have occurred to me to use XOR, even if I knew about this possibility.
As a result (unsuccessfully trying to fall asleep for two hours :) ), I got up, went to the computer and wrote a script that did a lot of comparisons in one way or another and measured the time.
It turned out that if you use bitwise XOR instead of != , there is a performance gain (at least on Firefox), this gain is stable (that is, it is always there, then more, then less), but it is only about 1%.
Maybe there are some other features that give an advantage to bitwise XOR?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
M
MagaSoft, 2010-09-26
@MagaSoft

>Last night I was thinking about this... unsuccessfully trying to fall asleep for two hours...
I think the author has achieved his goal.

G
garex, 2010-09-25
@garex

Only performance, because all this happens in a language understandable to the processor.
And in this case, it is rather an attack of phallometry.

M
murr, 2010-09-26
@murr

>> this increase is stable (that is, it is always there, sometimes more, sometimes less)
>> is only about 1%
1% growth is not an increase, but just a small outlier within the measurement method error.
Perhaps the author wanted to write

while (i --> -1)

but I thought it was too easy.

S
shergin, 2010-09-26
@shergin

Maybe in this way the code compresses the js-minimizer, for example, Google Closure?

A
Alexey Sidorov, 2010-09-26
@Gortauer87

As an option, the code was written by a fan of Olympiad programming. There the main task is to solve, meeting the standards, and it doesn’t matter how incomprehensible the decision will be.

P
Parsing, 2010-09-25
@Parsing

Maybe it has something to do with 64-bit int

S
sedictor, 2010-09-27
@sedictor

1% on how many iterations?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question