gpt4 book ai didi

javascript - JS数组中 "|"(管道)符号的用途是什么

转载 作者:数据小太阳 更新时间:2023-10-29 05:52:02 26 4
gpt4 key购买 nike

我有一个 JS 数组,在我们现有的代码中使用如下:

temp = charArray[0 | Math.random() * 26];

想知道“|”的具体用法上面代码中的符号,还有更多这样的运算符吗?

最佳答案

来自 the MDN :

Bitwise operators treat their operands as a set of 32 bits (zeros and ones) and return standard JavaScript numerical values.

由于32位部分是IEEE754的整数部分(的一部分)数字的表示,这只是删除数字的非整数部分的技巧(注意它也会破坏不适合 32 位的大整数!)。

相当于

temp = charArray[Math.floor(Math.random() * 26)];

关于javascript - JS数组中 "|"(管道)符号的用途是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30350734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com