Unusual use bitwise XOR in Javascript?

0 like 0 dislike
4 views
Hi.


Debugging recently, another piece of someone else's code I stumbled on this cycle:

while (--i ^ -1) /* loop body */;


in this case, initially, i is equal to some positive number. That is, the bitwise operator is used for integers. A little thought, I realized that the result of a bitwise XOR will be nonzero only in the case when numbers are equal. Then the condition in kastritsa true. Otherwise, it will be non-zero, which kastritsa in false.


It turns out, you can use the bitwise XOR operator instead of != (if we know in advance that the compared values are the numbers).


Here I have a question I want to put in front of an audience.


Why the author uses XOR?


Last night I pondered this and the most plausible cause, I found the desire to write "cool obscure code". Immediately I remembered how I was when I started to learn the basics of programming, suddenly discovered that can write "concise code that is difficult to understand and which works." It seemed to me that this is evidence of some skill. In fact, it is clear that this is evidence to the contrary. In favor of this hypothesis also had what the rest of the code in the area given snippet was written in a similar way.


However, I did not give rest to another thought. I imagined that if I wanted to complicate the code, I'd somehow not thought to use XOR, even if I knew about this possibility.


As a result (trying unsuccessfully to sleep for two hours :) ), I got up, went to my computer and I have written script that did a lot of comparisons to those other ways and measured the time.


It turned out that if you use a bitwise XOR instead of !=, you receive a boost in performance (at least on Firefox), this increase is stable (that is, he is always there, some more, some less), but is only around 1%.


Maybe there are still some features that gives the advantage of the bitwise XOR?
by | 4 views

7 Answers

0 like 0 dislike
>Last night I was thinking on this... trying unsuccessfully to sleep for two hours...
I think the author succeeded.
by
0 like 0 dislike
Only the performance for processor to clear language it all happens.
\r
And in this case, rather it is an attack of fallometrii.
by
0 like 0 dislike
>> this increase is stable (that is, he is always there, some more, some less)
>> is only about 1%
\r
1% increase is not an increase, just a little release within the error of the measurement method.
\r
Perhaps the author wanted to write
\r
while (i --> -1)

but decided that was too easy.
by
0 like 0 dislike
Maybe this way the code ouimet js minimizer, for example, Google Closure?
by
0 like 0 dislike
As a variant of the code wrote a fan olimpiadnogo programming. There is the main problem to solve to meet the standards, and how will not understand the solution no matter.
by
0 like 0 dislike
Maybe it has something to do with 64-bit int
by
0 like 0 dislike
1% how many iterations?
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
7 answers
0 like 0 dislike
4 answers
asked Apr 12, 2019 by Akaero
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
1,120 users