Bitwise operations on non numbers

bit-manipulation, javascript, operators

Solution

According to the ES5 spec, when doing bitwise operations, all operands are converted to `ToInt32` (which first calls `ToNumber`. If the value is `NaN` or `Infinity`, it's converted to `0`).

Thus: `NaN ^ 1` => `0 XOR 1` => `1`

Problem

Somehow, JavaScript makes sense of the bitwise operations `NaN ^ 1`, `Infinity ^ 1` and even `'a' ^ 1` (all evaluate to `1`). What are the rules governing bitwise operators on non numbers? Why do all the examples above evaluate to `1`?

Original source