3 ms·
I think a critical point to is that |0 forces integer arithmetic, beyond just ensuring that integer arithmetic is used to do the calculation. E.g. (1/2) |0 give
by hn_throwaway_99 4y ago
I think a critical point to is that |0 forces integer arithmetic, beyond just ensuring that integer arithmetic is used to do the calculation. E.g. (1/2) |0 gives a very different result than (1/2), so using |0 ensures that integer arithmetic is used even if you don't know what the operands are.
- qsort 4y agoI mean, no, not really, unless you're appending |0 to everything like a compiler would, i.e. ((a|0) / (b|0))|0. I'm not saying it's useless, asm.js used this trick to great effect. I'm just saying that unless the situation is more complex and doing a bitwise operation actually encodes information (like the &0xff example in a sibling comment, from which the compiler can deduce "this is a uint8"), it's unlikely you're giving the compiler more information than it's already capable of deducing on its own.
- Asraelite 4y ago> unless you're appending |0 to everything like a compiler would, i.e. ((a|0) / (b|0))|0. It effectively does do that though, since earlier in your code `a` and `b` would be defined with `let a = ... |0` so now `a` and `a|0` are equivalent in any subsequent expressions.
- chrisseaton 4y agoYes - local variables are just indirections through a name - in a compiler referring to a and referring to the original ... |0 expression are the same thing. https://twitter.com/ChrisGSeaton/status/1530579735012593665 https://twitter.com/ChrisGSeaton/status/1530579735012593665
- deleted 4y ago[deleted]