Why is ( Infinity | 0 ) === 0?
Asked Answered
A

2

23

I'm fiddling around with bitwise operators in JavaScript and there is one thing I find remarkable.

The bitwise or operator returns 1 as output bit if one of the two input bits are 1. So doing x | 0 always returns x, because | 0 has no effect:

  • ( 1 | 0 ) === 1
  • ( 0 | 0 ) === 0

However, when I calculated Infinity | 0, I got 0. This is surprising in my opinion, because by the above one should get Infinity. After all, ( x | 0 ) === x.

I cannot find where in the ECMAscript specification this is explicitly defined, so I was wondering what exactly implies that ( Infinity | 0 ) === 0. Is is perhaps the way Infinity is stored in memory? If so, how can it still be that doing a | 0 operation causes it to return 0 whereas | 0 should not do anything?

Alcheringa answered 11/7, 2011 at 12:29 Comment(4)
How could you compute (infinity | 0)? Infinity is... infinite, it can't be computed by its very definition... :|Idiophone
@Idiophone - Because a computer has to represent infinity somehow, and it's got a limited number of bits to do it with.Micelle
This is really interesting, because Infinity appears to be a truthy value when you use it in an if-else statement, like one would expect. I just confirmed this: jsfiddle.net/LWBVd. Perhaps it has to do with the internal representation of Infinity.Imogeneimojean
@FishBasketGordo: Looking at the answers it appears that values are converted to an integer first. Just like 0.1 is truthy, 0.1 | 0 is falsy.Alcheringa
Y
52

Bitwise operators work on integers only.
Infinity is a floating-point value, not an integer.

The spec says that all operands of bitwise operations are converted to integers (using the ToInt32 operation) before performing the operation.

The ToInt32 operation says:

If number is NaN, +0, −0, +∞ or –∞ return +0.

Yardstick answered 11/7, 2011 at 12:30 Comment(0)
M
2

Doing math and other operations that expect integers with NaN and Infinity is usually a bad idea. How would you set/clear a bit from Infinity?

Actually, bit-wise operations are only defined for integers - and integers do not have NaN or Infinity.

Microseism answered 11/7, 2011 at 12:30 Comment(5)
I was thinking that Infinity would represent an infinite amount of 1 bits after each other.Alcheringa
That's more like -1 than infinity (in that this is how -1 behaves in languages like python that have arbitrary size integers - javascript only has 32 bit integers.)Bissonnette
@Random832: I guess I was thinking too simplistically. My idea was that just like 1 is 1 and 1111 is 16, 111...111 would end up to Infinity.Alcheringa
1111 binary is 15. If you follow this line of reasoning an infinite value would occupy every bit in memory and would still not accurately portray infinity.Felicefelicia
@dbasnett: It's 15 indeed, my stupidity. I was thinking it would represent an infinite amount of ones so that Infinity | 0 === 111...111 | 0 === 111...111 === Infinity. But never mind, it's not the case :)Alcheringa

© 2022 - 2024 — McMap. All rights reserved.