Answer the question
In order to leave comments, you need to log in
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
>Last night I was thinking about this... unsuccessfully trying to fall asleep for two hours...
I think the author has achieved his goal.
Only performance, because all this happens in a language understandable to the processor.
And in this case, it is rather an attack of phallometry.
>> 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)
Maybe in this way the code compresses the js-minimizer, for example, Google Closure?
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question