Javascript pipe in math to get Math.floor without using Math.floor

This question already has an answer here:

  • Using bitwise OR 0 to floor a number 5 answers

  • A single pipe | is BitWise OR.
    Bitwise operator only allow integer values, so after decimal point value is discarded.

    Bitwise OR operator | takes 2 bit patterns, and perform OR operations on each pair of corresponding bits.
    The following example will explain it.

    1010                            
    1100       
    ----------bitwise or
    1110       
    

  • Firstly the result of (9+2)/2 is 5.5 in JavaScript
  • Then it's applied a bitwise Or operation. For JavaScript, the bitwise operations don't work directly on the 64-bit representations. Instead, the value is converted into a 32-bit integer, which means 5.5 to 5 , then the result of 5|0 is 5 .
  • 链接地址: http://www.djcxy.com/p/77434.html

    上一篇: )在Javascript中的作用与parseInt相同吗?

    下一篇: 数学中的Javascript管道可以在不使用Math.floor的情况下获得Math.floor