How bitwise XOR works

Suppose we want to take a bitwise XOR of 5 and 12:

  1. Covert both numbers to binary; 5 is 0101 and 12 is 1100.
  2. Bitwise XOR performs an XOR operation bit by bit, starting from the first bit: 5  ^  12 0 ^ 1 = 1 1 ^ 1 = 0 0 ^ 0 = 0 1 ^ 0 = 1
  3. Thus, 5 ^ 12 = 1001, which is 9.

With all the knowledge we required about atomics, let's hop back to our one billion count problem and see a possible fix.

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.