gpt4 book ai didi

javascript - 无法理解 | 的用法JavaScript 中的运算符

转载 作者:行者123 更新时间:2023-12-03 02:47:04 24 4
gpt4 key购买 nike

我是 java 脚本新手,无法理解 | 的用法以下代码片段中的运算符

<!DOCTYPE html>
<html>
<body>

<p>Click the button to display a number between 0 and 1</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
var a=Math.random();
var x = ((a*2 | 0));
document.getElementById("demo").innerHTML = x;

}
</script>
</body>

</html>

最佳答案

那就是 bitwise OR operator

Performs the OR operation on each pair of bits. a OR b yields 1 if either a or b is 1. The truth table for the OR operation is:

任意数字 x 与 0 进行按位或运算,得到 x。对任何数字 x 与 -1 进行按位或运算会得到 -1。

关于javascript - 无法理解 | 的用法JavaScript 中的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48056960/

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