How does the bitwise XOR operator work in JavaScript?

Why is it that after 11100, 420 pops up instead of 00100, but the final value (11000) still matches the original one?

var sir = 11000;
alert(sir);
var meg = 11100;
alert(meg);
var tun = sir ^ meg;
alert(tun);
alert(tun ^ meg);
Author: Overground, 2019-01-27

1 answers

You write 2 numbers in decimal notation. The operation ^(XOR) is performed on the bits (!) that represent these 2 numbers.

I.e.


1100010 = 101010111110002

1110010 = 101011010111002

Applying the ^ operation to them, we get the following:

          000001101001002 = 42010


"But the final value (11000) still corresponds to the original value." is one of the properties of the XOR operation: (a XOR b) XOR b = a
These are the hidden subtleties of programming, so to speak. In my own time, I had a long time to catch up with what was what and how.
 3
Author: Дмитрий, 2019-01-27 07:52:09