# Why NaN ^ NaN == 0?

31

Look at these tests I made on the browser console:

``````NaN / NaN
NaN

NaN * NaN
NaN

typeof NaN
"number"

NaN ** NaN
NaN

NaN ^ NaN
0
``````

Why exactly does this happen?

asked by anonymous 08.06.2017 / 13:13

27

According to MDN :

NaN - The global NaN property is a special value that means Not-A-Number (not a number).

According to the specifications ECMAScript 5 (index 9.5) and ECMA 262 (index 7.1.5), when we do a bitwise operation all the elements involved are converted to 32-bit integers ( ToInt32 ). If the value is one of the global properties NaN or infinity the value is converted to 0.

2 . If number is NaN , +0, -0, + ∞, or -∞, return +0.

Knowing that the `^` operator represents the logical operation `XOR` ...

`NaN ^ NaN` is equivalent to `0 XOR 0`

``````console.log("NaN ^ NaN: " + (NaN ^ NaN));
console.log("0 ^ 0: " + (0 ^ 0));
console.log("Infinity ^ 0: " + (Infinity ^ 0));``````

08.06.2017 / 13:56
15

Concept:

What is NaN

NaN is a property of the global object.

The initial value of NaN is Not-A-Number - the same value as Number.NaN. In modern browsers, NaN is a read-only and non-configurable property. Even when this is not the case, avoid overwriting it.

Testing a NaN value

Equality operators (== and ===) can not be used to test a NaN value. Instead, use `Number.isNaN()` or `isNaN()` .

``````NaN === NaN;        // falso
Number.NaN === NaN; // falso
``````

XOR Port

Exclusive or exclusive disjunction , generally known as XOR or EXOR , is a logical operation between two operands that results in a true logical value if and only if exactly one of the operands has true value. It can be synthesized as a difference detector between two logical operands. Font

JavaScript

Bitwise Operators

XOR (a ^ b)

Returns a 0 for each position where the bits of the corresponding position are the same. [Returns a 1 for each position where the corresponding position bits are different.]

Conceptually, logical bitwise operators work as follows:

• Operands are converted to 32-bit integers and expressed as a series of bits (zeros and ones). Numbers with greater than 32 bits will have their bits truncated. For example, the following integer has binary representation greater than 32 bits will be converted into a 32-bit integer.

• Before: 11100110111110100000000000000110000000000001
•
• Next: 10100000000000000110000000000001
•
• Each bit of the first operand is paired with the corresponding bit of the second operand: first bit with first bit, second bit with second bit, and so on.
• The operator is applied to each pair of bits and the result is built bit by bit.

For example, the binary representation of nine is 1001 and the binary representation of fifteen is 1111. Thus, when bitwise operators are applied to these values, the results are as follows:

Example:

Expression: 15 ^ 9

Result: 6

Comparison in binary: 1111 ^ 1001

Binary result: 0110

Because NaN is Not-A-Number, it is not possible to perform arithmetic operations because it will always result in NaN.

``````NaN / NaN
NaN

NaN * NaN
NaN

NaN ** NaN
NaN
``````

In bitwise operators, as an example, XOR will compare NaN by always being 0.

``````NaN ^ NaN // 0 ^ 0
0
``````

And when comparing `NaN ^ 2` will be comparing 00B with 10B (B = Binary), returning 10B which converting to decimal is 2

08.06.2017 / 14:01
11

In the case of arithmetic operators, NaN is not calculable and any value with one of these operators used in partnership with NaN will return NaN. Since NaN is not a number.

``````NaN + 1 = NaN

NaN / 2 = NaN
``````

In the case of XOR, which is a bitwise operator, you can test the calculation based on:

• For each bit in the same position and with the same value, it yields: 0;
• For each bit in the same position and with a different value, it results: 1.

`NaN ^ NaN = 0`

Assuming:

`NaN = 0` then:

``````0 ^ 0 = 0
``````

If you test:

`1 ^ 2 = 3` , why?

Let's look at the binary representation of 1 and 2:

• 1 = 0001;
• 2 = 0010;

Applying XOR logic:

• 1st pos 1 = `0` and 1st pos 2 = `0` = `0` ;
• 2nd pos 1 = `0` and 2nd pos 2 = `0` = `0` ;
• 3rd pos 1 = `0` and 3rd pos 2 = `1` = `1` ;
• 4th pos 1 = `1` and 4th pos 2 = `0` = `1` ;

logo: `0001 ^ 0010 = 0011` , where `0011` is the binary representation of number 3.

08.06.2017 / 14:00
7

The global NaN property is a special value that means Not-A-Number (not a number).

Unlike all other possibilities of values in JavaScript, it is not possible to trust equality operators (== and ===) to determine whether the value is NaN or not, because both NaN == NaN and NaN === NaN, returns: false. Hence the need for the isNAN 'isNAN ()' function.

08.06.2017 / 13:45
2

`NaN ^ NaN = 0` Why exactly does this happen?

For laziness, oblivion, bug, chance!

`NaN ^ NaN` should give `NaN`

The `NaN ^ NaN` operation does not make much sense - bitwise operations with "uncontrolled" values were not predicted by those who did this language implementation, making the default implementation was used.

The default implementation can be one of two assumptions:

h1) coercion comparison for integers:

``````int(NaN) ^ int(NaN) => 0 ^ 0 => 0
``````

h2) xor bib direct bit: In the representation of floating point numbers, a conventionally represented representation for NaN, associated to a set of bits, was chosen. By doing bitwise XOR of two equal groups gives 0

14.07.2017 / 14:56