| 这个运算符在js里面有什么作用?
[b]问题补充:[/b]
var num = 11/10|0
这个num得到什么,如果按位于的话。呵呵。
还有答案吗?
[b]问题补充:[/b]
可以把2/10|0就原理说一下吗?
[quote]Bitwise OR (|)
The | operator performs a Boolean OR operation on each bit of its integer arguments. A bit is set in the result if the corresponding bit is set in one or both of the operands. For example, 9 | 10 evaluates to 11.
Division (/)
The / operator divides its first operand by its second. If used with nonnumeric operands, it attempts to convert them to numbers. If you are used to programming languages that distinguish between integer and floating-point numbers, you might expect to get an integer result when you divide one integer by another. In JavaScript, however, all numbers are floating-point, so all division operations have floating-point results: 5/2 evaluates to 2.5, not 2. Division by zero yields positive or negative infinity, while 0/0 evaluates to NaN.[/quote]
2/10|0,首先计算2/10, 结果为0.2
然后计算0.2|0, |首先会把0.2转换为整数,去掉小数点后的数字。然后与0做按位于操作。即:
00000000
|
00000000
|,按位或的意思。
或的意思,逻辑或,意思是:
“|”两边的表达式都要执行的;
“||”如果左边的表达式是true的话,那么右边的表达式是不会被执行的;
例如:
var i = 0;
if(1==1 | i++=1){
alert(i); // 这里的i为1,即i++==1这个表达式被执行了
}
var j = 0;
if(1==1 || j++=1){
alert(j); // 这里的j为0,即j++==1这个表达式没有被执行
}
希望你能明白,“||”为短路或,“&”和“&&”的用法是类似的。
11/10|0=(11/10)|0=1|0=1