We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
以前取随机数的方式一直是
const rIndex = Math.floor(Math.random() * arr.length);
实际上可以使用位运算代替 Math.floor
Math.floor
const rIndex = Math.random() * arr.length | 0;
这是因为要进行运算的值会被先转为有符号的 32 位整型。所以超过 32 位的整数会被截断,而小数部分则会被直接舍弃。
除此之外,还有哪些好用的位运算使用方式呢?
The text was updated successfully, but these errors were encountered:
n & 1 === 0
~~3.14 // 3
1 << 3 // 2^3 = 8 1 << 4 // 2^4 = 16
Sorry, something went wrong.
No branches or pull requests
以前取随机数的方式一直是
实际上可以使用位运算代替
Math.floor
这是因为要进行运算的值会被先转为有符号的 32 位整型。所以超过 32 位的整数会被截断,而小数部分则会被直接舍弃。
除此之外,还有哪些好用的位运算使用方式呢?
The text was updated successfully, but these errors were encountered: