JavaScript |操作者 [英] JavaScript | operator

查看:81
本文介绍了JavaScript |操作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都能解释什么是|和之后的价值呢?我知道0的输出会创建13个数组,数字为3,2,1,0。但是什么呢? 1,或者2。

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);
}


推荐答案

这是一种聪明的方式完成相同的效果:

This is a clever way of accomplishing the same effect as:

Math.floor(i/13);

JavaScript开发人员似乎擅长这些东西:)

JavaScript developers seem to be good at these kinds of things :)

在JavaScript中,所有数字都是浮点数。没有整数类型。所以即使你这样做:

In JavaScript, all numbers are floating point. There is no integer type. So even when you do:

 var i = 1;

我真的是浮点数 1.0 。所以如果你只是做了 i / 13 ,你最终得到它的一小部分,输出将是 3.846 ... 例如。

i is really the floating point number 1.0. So if you just did i/13, you'd end up with a fractional portion of it, and the output would be 3.846... for example.

在JavaScript中使用bitwise或运算符时,运行时必须将操作数转换为32位整数才能继续。这样做可以消除小数部分,只留下一个整数。按位或零是一个无操作(好吧,在一个真正整数的语言中没有操作)但是在JavaScript中有地板的副作用。

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.

这篇关于JavaScript |操作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆