admin管理员组

文章数量:1208153

In the accepted answer on my earlier question ( What is the fastest way to generate a random integer in javascript? ), I was wondering how a number loses its decimals via the symbol | .

For example:

var x = 5.12042;
x = x|0;

How does that floor the number to 5?

Some more examples:

console.log( 104.249834 | 0 ); //104
console.log( 9.999999 | 0 );   // 9

In the accepted answer on my earlier question ( What is the fastest way to generate a random integer in javascript? ), I was wondering how a number loses its decimals via the symbol | .

For example:

var x = 5.12042;
x = x|0;

How does that floor the number to 5?

Some more examples:

console.log( 104.249834 | 0 ); //104
console.log( 9.999999 | 0 );   // 9
Share Improve this question edited May 23, 2017 at 10:34 CommunityBot 11 silver badge asked Jan 28, 2012 at 23:53 auroranilauroranil 2,6616 gold badges26 silver badges35 bronze badges 4
  • 2 You should be aware that using bitwise operators will limit you to 32-bit signed integers. ((Math.pow(2,32)/2)-1)|0; // 2147483647 Remove the -1 and you'll not get the desired result. ((Math.pow(2,32)/2))|0; // -2147483648 – user1106925 Commented Jan 28, 2012 at 23:57
  • Interesting. That's probably the reason that this function is slightly faster than the Math.floor(x) function. jsperf.com/floor-or-or – auroranil Commented Jan 30, 2012 at 3:01
  • 2 it is not actually 'flooring', try with -1.23 to see what happens – ajax333221 Commented Jun 5, 2012 at 3:53
  • Yes, truncating is the correct term in this case. @ajax333221 – user12582392 Commented Jun 5, 2023 at 12:41
Add a comment  | 

3 Answers 3

Reset to default 18

Because, according to the ECMAScript specifications, bitwise operators call ToInt32 on each expression to be evaluated.

See 11.10 Binary Bitwise Operators:

The production A : A @B, where @ is one of the bitwise operators in the productions above, is evaluated as follows:

  1. Evaluate A.

  2. Call GetValue(Result(1)).

  3. Evaluate B.

  4. Call GetValue(Result(3)).

  5. Call ToInt32(Result(2)).

  6. Call ToInt32(Result(4)).

  7. Apply the bitwise operator @ to Result(5) and Result(6). The result is a signed 32 bit integer.

  8. Return Result(7).

Bitwise operators convert their arguments to integers (see http://es5.github.com/#x9.5). Most languages I know don't support this type of conversion:

    $ python -c "1.0|0"
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unsupported operand type(s) for |: 'float' and 'int'

    $ ruby -e '1.0|0'
    -e:1:in `': undefined method `|' for 1.0:Float (NoMethodError)

    $ echo "int main(){1.0|0;}" | gcc -xc -
    : In function ‘main’:
    :1: error: invalid operands to binary | (have ‘double’ and ‘int’)

When doing a floor, although it would be possible to convert the argument to an integer, this is not what most languages would do because the original type is a floating-point number.

A better way to do it while preserving the data type is to go to exponent digits into the mantissa and zero the remaining bits.

If you're interested you can take a look at the IEEE spec for floating point numbers.

本文标签: How does x0 floor the number in JavaScriptStack Overflow