Home >Web Front-end >JS Tutorial >Flooring Floats with Bitwise OR: Faster Than Math.floor()?
Flooring Numbers with Bitwise OR 0
A unique method has surfaced for flooring float numbers using a bitwise OR operator:
var a = 13.6 | 0; // a == 13
Let's delve into this technique, exploring its mechanisms and comparing it to the standard Math.floor function.
How Does It Work?
Bitwise operations operate on signed 32-bit integers. Therefore, applying a bitwise OR to a float number effectively converts it to an integer, truncating the fractional part.
Advantages Over Math.floor?
Benchmarking results suggest that this method can be marginally faster than Math.floor.
Disadvantages:
Comparative behavior differs from Math.floor:
The above is the detailed content of Flooring Floats with Bitwise OR: Faster Than Math.floor()?. For more information, please follow other related articles on the PHP Chinese website!