JavaScript |操作员
谁能解释一下什么是“|”和之后的值呢?我知道 0 的输出会创建 13 个集合,即数字 3、2、1、0。但是 | 呢? 1,或 | 2.
var i = 52;
while(i--) {
alert(i/13 | 0);
}
Anyone able to explain what "|" and the value after does? I know the output for 0 creates sets of 13, the numbers, 3, 2, 1, 0. But what about | 1, or | 2.
var i = 52;
while(i--) {
alert(i/13 | 0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它是按位或运算符。 MDC 提供了解释和示例。由于对一个操作数为 0 进行按位“或”会产生另一个操作数的值,因此在这种情况下,它
什么都不做将除法的结果向下舍入。如果写成
| 1
它所做的总是打印奇数(因为它将把 1 位设置为 on);具体来说,它会导致偶数加 1,而奇数不变。更新:正如评论者正确指出的那样,按位运算符会导致两个操作数都被视为整数,因此会删除除法结果的任何分数。我纠正了。
It is the bitwise OR operator. There is both an explanation and an example over at MDC. Since doing bitwise OR with one operand being 0 produces the value of the other operand, in this case it
does exactly nothingrounds the result of the division down.If it were written
| 1
what it would do is always print odd numbers (because it would set the 1-bit to on); specifically, it would cause even numbers to be incremented by 1 while leaving odd numbers untouched.Update: As the commenters correctly state, the bitwise operator causes both operands to be treated as integers, therefore removing any fraction of the division result. I stand corrected.
这是实现相同效果的巧妙方法:
JavaScript 开发人员似乎擅长此类事情:)
在 JavaScript 中,所有数字都是浮点数。没有整数类型。因此,即使您这样做:
i实际上是浮点数
1.0
。因此,如果您只执行i/13
,您最终会得到其中的小数部分,例如,输出将为3.846...
。在 JavaScript 中使用按位或运算符时,运行时必须将操作数转换为 32 位整数,然后才能继续。这样做会去掉小数部分,只留下一个整数。按位或为零是一个无操作(好吧,在具有真整数的语言中是无操作),但在 JavaScript 中具有地板的副作用。
This is a clever way of accomplishing the same effect as:
JavaScript developers seem to be good at these kinds of things :)
In JavaScript, all numbers are floating point. There is no integer type. So even when you do:
i is really the floating point number
1.0
. So if you just didi/13
, you'd end up with a fractional portion of it, and the output would be3.846...
for example.When using the bitwise or operator in JavaScript, the runtime has to convert the operands to 32 bit integers before it can proceed. Doing this chops away the fractional part, leaving you with just an integer left behind. Bitwise or of zero is a no op (well, a no op in a language that has true integers) but has the side effect of flooring in JavaScript.
它是一个按位运算符。具体来说是OR 位运算符。
它的基本作用是将您的 var 用作位数组,并且每个相应的位彼此相连。如果其中任何一个为 1,则结果为 1。如果两者均为 0,则结果为 0。
示例:
24 = 11000
10 = 1010
两者的长度不相等,因此我们用 0 进行填充
24 = 11000
10 = 01010
26 = 11010
24 | 10 = 26
学习这一点的最佳方法是阅读。
It's a bitwise operator. Specifically the OR Bitwise Operator.
What it basically does is use your var as an array of bits and each corresponding bit is with eachother. The result is 1 if any of them is 1. And 0 if both are 0.
Example:
24 = 11000
10 = 1010
The two aren't of equal length so we pad with 0's
24 = 11000
10 = 01010
26 = 11010
24 | 10 = 26
Best way to learn this is to readup on it.
这就是按位或。在计算表达式时,LHS 被截断为整数并返回,因此 |实际上与 Math.floor() 相同。
That is the bitwise OR. In evaluating the expression, the LHS is truncated to an integer and returned, so | is effecively the same as Math.floor().