In js, in the past, the Math.floor method was used to round down, but now we see this usage: OR operation interval = interval | 0 Why can we round down in this way? , what are the advantages of this usage compared with Math.floor?
Note that|is not a logical OR, but a bitwise OR (OR).
Some small differences. For example,Math.floor(NaN)still returnsNaN. ButNaN | 0returns 0. Another example isMath.floor(Infinity)returnsInfinity, butInfinity | 0returns 0
First of all, S1ngS1ng upstairs is right about those small differences.
In addition,|is a bitwise OR operation. Since when0is stored in the memory, all integer bits are filled with 0, so the OR operation is performed based on the binary bit and a numerical value. Regardless of the corresponding bit Whether0or1is ORed with0, you will get itself. However, since the number 0 does not have a decimal part in the memory, the decimal part ofintervalis discarded after the bitwise OR operation. In fact, rounding down is achieved by discarding the decimal part.
Since it is a bit operation, it will be faster thanMath.floor().
The real reason is: automatic type conversion within js.
js values are all expressed in64-bitfloating point type. When a value requiresbit operations, js will automatically convert it into a32-bit signedinteger and discard the decimal part.
n|0; n>>0; //The following 0 is only used to ensure that the integer value of n remains unchanged.
Reducing from 64-bit to 32-bit will cause a loss of accuracy.Be careful!, maximum effective range: 2^32/2-1
Both can be achieved,interval = interval | 0This is a writing technique, it depends on personal preference. It may be thatinterval = interval | 0will run faster, and writing code will definitely be faster thanMath.floor!
Note that
|
is not a logical OR, but a bitwise OR (OR).Some small differences. For example,
Math.floor(NaN)
still returnsNaN
. ButNaN | 0
returns 0.Another example is
Math.floor(Infinity)
returnsInfinity
, butInfinity | 0
returns 0You can also do this
interval = interval >> 0
First of all, S1ngS1ng upstairs is right about those small differences.
In addition,
|
is a bitwise OR operation. Since when0
is stored in the memory, all integer bits are filled with 0, so the OR operation is performed based on the binary bit and a numerical value. Regardless of the corresponding bit Whether0
or1
is ORed with0
, you will get itself. However, since the number 0 does not have a decimal part in the memory, the decimal part ofinterval
is discarded after the bitwise OR operation. In fact, rounding down is achieved by discarding the decimal part.Since it is a bit operation, it will be faster than
Math.floor()
.The real reason is: automatic type conversion within js.
js values are all expressed in
64-bit
floating point type. When a value requiresbit operations
, js will automatically convert it into a32-bit signed
integer and discard the decimal part.n|0; n>>0; //The following 0 is only used to ensure that the integer value of n remains unchanged.
Reducing from 64-bit to 32-bit will cause a loss of accuracy.
Be careful!
, maximum effective range: 2^32/2-1Both can be achieved,
interval = interval | 0
This is a writing technique, it depends on personal preference. It may be thatinterval = interval | 0
will run faster, and writing code will definitely be faster thanMath.floor
!