Why not "|" Does the operator work for large values?

I looked at this question on how to effectively check if a value is an integer. One answer is recommended n === (n|0)for quick rounding using an operator |. On a whim, I decided to test it with Number.MAX_VALUE. Despite the fact that it should be an integer (I think?), The Test returned with an error.

It also returned to false for Number.MAX_SAFE_INTEGER, so I decided to check out some other large numbers and found the following:

Number.MAX_VALUE | 0           --> 0
Number.MAX_SAFE_INTEGER | 0   --> -1
Number.MAX_SAFE_INTEGER/2 | 0 --> -1
Number.MAX_SAFE_INTEGER/8 | 0 --> -1
1234567890 | 0                --> 1234567890 

I'm not sure what the operator |does internally, but MAX_SAFE_INTEGERdoes not seem safe. Why is this so?

+4
source share
2 answers

, MAX_SAFE_INTEGER ... float , .. 53 .

, 32- .

, , ;)

+1

| JavaScript 32- , . |0 , , JavaScript 32- ( , int C , , ).

, Number.MAX_SAFE_INTEGER ; IE, undefined, , , , , , 32 , double ( ): 2 ^ 32- 1, 4 32 - . , | /signed/number, 31 . .

( , ), , . 0001, 1110 + 1 == 1111 (, 32 , ), , .

+3

Source: https://habr.com/ru/post/1536811/


All Articles