I'm a newbie in one of a sport programming service and found that winning solutions often use bitwise operators.

Here is an example.

Write a function, which finds a difference between two
arrays (consider that they differ by one element).

The solutions are:

let s = x => ~eval(x.join`+`);
let findDiff = (a, b) => s(b) - s(a);

and

let findDiff = (a, b) => eval(a.concat(b).join`^`);

I would like to know:

The explanation of those two examples (bitwise part).

What's the advantage of using bitwise operators on decimal numbers?

Is that faster to operate with bitwise operations rather than normal one?

Update:
I didn't fully understand why my question is marked as a duplicate to ~~ vs parseInt. That's good to know, why this operator replaces parseInt and probably helpful for sport programming. But it doesn't answer on my question.

Not necessarily faster. They are concise (and usually harder to read, but that's side effect). Code golf isn't focused on bitwise operators, is about code length. You may consider the relevant community instead of SO for this type of questions.
– estusJan 31 '17 at 6:06

1 Answer
1

Bitwise operators are not necessary faster, but generally fast enough. They are concise (and usually harder to read, but that's side effect).

~~ is a shorter (and usually more preformant) alternative to parseIntwith a considerable number of remarks. In regular (non-golf) code it should be used only if it provides the behaviour that is more desirable than parseInt or in performance-sensitive context.

~a is roughly equal to parseInt(a) * -1 - 1. It can be used as a shorter alternative to ~~a in this particular example, s(b) - s(a), because * -1 - 1 part is eliminated on subtraction (the sign should be taken into account).